Android开发

Retrofit结合RXJAVA的使用

2018-09-11  本文已影响57人  sain_wu

本文参考: https://www.jianshu.com/p/5bc866b9cbb9

一.Retrofit结合RXJAVA的使用

以获取豆瓣 Top250 榜单为例,取前10 , 地址如下

https://api.douban.com/v2/movie/top250?start=0&count=10

1.首先,要使用Retrofit ,你肯定需要把它的包引入,在你的build.gradle文件中添加如下配置:

 compile 'com.squareup.retrofit2:retrofit:2.1.0'//retrofit 
 compile 'com.google.code.gson:gson:2.6.2'//Gson 库 
//下面两个是RxJava 和RxAndroid 
compile 'io.reactivex:rxjava:1.1.0' 
compile 'io.reactivex:rxandroid:1.1.0'  
compile 'com.squareup.retrofit2:converter-gson:2.1.0'//转换器,请求结果转换成Model 
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'//配合Rxjava 使用

2.创建一个Retrofit 实例,并且完成相关的配置

public static final String BASE_URL = "https://api.douban.com/v2/movie/";
 // 创建Retrofit
        mRetrofit = new Retrofit.Builder()
                .client(builder.build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ApiConfig.BASE_URL)
                .build();

说明:配置了接口的baseUrl和一个converter,GsonConverterFactory 是默认提供的Gson 转换器,Retrofit 也支持其他的一些转换器,详情请看官网Retrofit官网
3,创建一个 接口, 代码如下:

public interface MovieService{
    //获取豆瓣Top250 榜单  call类型
    @GET("top250")
    Call<MovieSubject> getTop250(@Query("start") int start,@Query("count")int count);

   //获取豆瓣Top250 榜单  observable类型
    @GET("top250")
    Observable<MovieSubject> getTop250(@Query("start") int start, @Query("count") int count);
}

说明:

https://api.douban.com/v2/movie/top250?start=0&count=10

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

4,用Retrofit 创建接口实例MoiveService,并且调用接口中的方法进行网络请求

//获取接口实例
MovieService MovieService movieService = retrofit.create(MovieService.class); 
//调用方法得到一个Call 
Call<MovieSubject> call = movieService.getTop250(0,20);
 //进行网络请求 
call.enqueue(new Callback<MovieSubject>() {
       @Override 
       public void onResponse(Call<MovieSubject> call, Response<MovieSubject> response) { 
            mMovieAdapter.setMovies(response.body().subjects);     
            mMovieAdapter.notifyDataSetChanged(); 
       } 
      @Override 
      public void onFailure(Call<MovieSubject> call, Throwable t) { 
         t.printStackTrace(); 
      } 
});

以上是异步方式请求,还有同步方式execute(),返回一个Response,代码如下:
Response<MovieSubject> response = call.execute();

public Observable<List<Movie>> getMovie(int start, int count) {
    return  mRetrofit.create(MovieService.class).getTop250(start, count)
            .subscribeOn(Schedulers.io())
            .map(new Func1<MovieSubject, List<Movie>>() {
                @Override
                public List<Movie> call(MovieSubject movieSubject) {
                    return movieSubject.subjects;
                }
            }).observeOn(AndroidSchedulers.mainThread());
    }

map方法完成对事件序列的变换, 输入为MovieSubject, 输出为List<Movie>
MovieSubject定义如下

public class MovieSubject {
    public int count;
    public int start;
    public int total;
    public List<Movie>  subjects;
    public String title;
}

至于为啥要定义一个这样的类型, 其实这个是由API的返回数据决定的, 借助Chrome调试或者Charles抓包工具,可以看到

Chrome 返回数据
count:10
start:0
subjects:[{rating: {max: 10, average: 9.6, stars: "50", min: 0}, genres: ["犯罪", "剧情"], title: "肖申克的救赎",…},…]
title:"豆瓣电影Top250"
total:250

Charles抓包数据
count   Integer 10
start   Integer 0
total   Integer 250
subjects    Array   
title   String  豆瓣电影Top250

要注意的是, 定义的元素个数可以和和json返回数据不一致,但元素名必须完全一致,下面的定义也是可以的

public class MovieSubject {
    public List<Movie>  subjects;
    public String title;
}
  1. 加入 OkHttp 配置
    通过OkHttpClient 可以配置很多东西,比如链接超时时间,缓存,拦截器等等。代码如下:
   // 创建 OKHttpClient
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(DEFAULT_CONNECT_TIME_OUT, TimeUnit.SECONDS);
    builder.writeTimeout(DEFAULT_WR_TIME_OUT, TimeUnit.SECONDS);
    builder.readTimeout(DEFAULT_WR_TIME_OUT, TimeUnit.SECONDS);

    // 添加公共参数拦截器
    HttpCommonInterceptor commonInterceptor = new HttpCommonInterceptor.Builder()
            .addHeaderParams("platform","android")
            .addHeaderParams("userToken","1234343434dfdfd3434")
            .addHeaderParams("userId","123445")
            .build();
    builder.addInterceptor(commonInterceptor);
    
    // 创建Retrofit,加入我们配置过的OKHttpClient
        mRetrofit = new Retrofit.Builder()
                .client(builder.build())
    

加拦截器的请求Contents:

GET /v2/movie/top250?start=0&count=10 HTTP/1.1
userToken   1234343434dfdfd3434
userId  123445
platform    android
Host    api.douban.com
Connection  Keep-Alive
Accept-Encoding gzip
User-Agent  okhttp/3.3.0

不加拦截器的请求Contents:

GET /v2/movie/top250?start=0&count=10 HTTP/1.1
Host    api.douban.com
Connection  Keep-Alive
Accept-Encoding gzip
User-Agent  okhttp/3.3.0

需要说明的是, 不配置Client, Retrofit也会创建一个默认OkHttpClient并使用, 源码为证:

    /**
     * Create the {@link Retrofit} instance using the configured values.
     * <p>
     * Note: If neither {@link #client} nor {@link #callFactory} is called a default {@link
     * OkHttpClient} will be created and used.
     */
    public Retrofit build() {

      okhttp3.Call.Factory callFactory = this.callFactory;
      if (callFactory == null) {
        callFactory = new OkHttpClient();
      }

      return new Retrofit(callFactory, baseUrl, converterFactories, adapterFactories,
          callbackExecutor, validateEagerly);
    }

二, 效果和源码参考

https://github.com/hixin/RXJAVADemo

device-2018-09-11-175412.png
上一篇 下一篇

猜你喜欢

热点阅读