Retrofit 使用

Retrofit是square出品的一个比较有名的开源网络请求库。

现在最新的版本是2.0

下面就说说其使用方法。

1.    首先定义一个承载接收数据的接口。如下

其中@GET后面是请求数据的子URL,可以是组合的URL,如下demo即是组合URL,owner和repo是不固定的。Contributor是服务器和终端之间的通信的数据结构

public interface GitHub {

   @GET("/repos/{owner}/{repo}/contributors")

   Call<List<Contributor>> contributors(

       @Path("owner") String owner,

       @Path("repo") String repo);

  }

public static class Contributor {

    public final String login;

    public final intcontributions;

 

    public Contributor(Stringlogin, int contributions) {

      this.login = login;

      this.contributions =contributions;

    }

  }

2.    New一个client,由于demo返回的是json数据,所以我们可以用Gson去解析,retrofit本身是没有转换器的,但是square提供了一些常用数据的jar包

·       Gson: com.squareup.retrofit2:converter-gson

·      Jackson: com.squareup.retrofit2:converter-jackson

·      Moshi: com.squareup.retrofit2:converter-moshi

·      Protobuf: com.squareup.retrofit2:converter-protobuf

·      Wire: com.squareup.retrofit2:converter-wire

·      SimpleXML: com.squareup.retrofit2:converter-simplexml

·      Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars

我们可以采用addConverterFactory把转换器加进去,如果数据格式并不在上面的常用数据格式里面,我们也可以通过实现Converter.Factory接口来创建一个自定义的converter。

// Create a very simple REST adapter which points the GitHub API.

    Retrofit retrofit = newRetrofit.Builder()

        .baseUrl(API_URL)

       .addConverterFactory(GsonConverterFactory.create())

        .build();

3.    创建接口请求实例

// Create an instance of our GitHub API interface.

    GitHub github =retrofit.create(GitHub.class);

 

    // Create a call instancefor looking up Retrofit contributors.

    Call<List<Contributor>> call =github.contributors("square", "retrofit");

4.    获取数据,分同步请求和异步请求

同步请求

1

2

3

4

// Synchronous Call in Retrofit 2.0

  

List<Contributor> contributors = call.execute().body();

以上的代码会阻塞线程,因此你不能在安卓的主线程中调用,不然会面临NetworkOnMainThreadException。如果你想调用execute方法,请在后台线程执行。

异步请求

1

2

3

4

5

6

7

8

9

10

11

12

13

14

// Synchronous Call in Retrofit 2.0

  

call.enqueue(List<Contributor> () {

    @Override

    public void onResponse(Response List<Contributor> response) {

        // Get result Repo from response.body()

    }

  

    @Override

    public void onFailure(Throwable t) {

  

    }

});

以上代码发起了一个在后台线程的请求并从response response.body()方法中获取一个结果对象。注意这里的onResponseonFailure方法是在主线程中调用的。

 

5.     

 

demo地址

https://github.com/ouclbc/Retrofit2.0_demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值