Retrofit+RxJava的联合使用

时间:2022-09-16 17:47:06

依赖库

//rxjava和retrofit
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.jakewharton:butterknife:7.0.1'

在清单文件中配置权限

<uses-permission android:name="android.permission.INTERNET" />
  • 创建接口,配置参数

以此接口为例 https://api.douban.com/v2/movie/top250?start=0&count=10

public interface RxjavaService {
@GET("top250")
Observable<RetrofitEntity> getRxjavaData(@Query("start") int start, @Query("count") int count);
}

如果只是retrofit,而没有用到rxjava时接口可以写成:

public interface RetrofitService {
@GET("top250")
Call<RetrofitEntity> getRetrofitData(@Query("start")int start,@Query("count")int count);
}

创建javabean类 RetrofitEntity
因为是get请求,可以在网页上获取数据,然后生成javabea类’

在mainActivity中的使用

private void gedatas() {
//添加rxjava后的请求
String baseUrl = "https://api.douban.com/v2/movie/";
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RxjavaService server = retrofit.create(RxjavaService.class);
server.getRxjavaData(0,10)
.subscribeOn(Schedulers.io())
.observeOn((AndroidSchedulers.mainThread()))
.subscribe(new Subscriber<RetrofitEntity>() {
@Override
public void onCompleted() {

}
@Override
public void onError(Throwable e) {

}
@Override
public void onNext(RetrofitEntity retrofitEntity) {
mTextview.setText(retrofitEntity.getSubjects().get(0).getTitle());
}
});
}

正常情况下,到此处就结束了,撒有哪啦

封装

  • 将retrofit和rxjavaserver放在同一个方法中
public class HttpMethods {
public static final String BASE_URL="https://api.douban.com/v2/movie/";
private static final int DEFAULT_TIMEOUT=5;
private Retrofit mRetrofit;
private RxjavaService mRxjavaService;

private HttpMethods(){
OkHttpClient.Builder httpClientBuilder = new OkHttpClient().newBuilder();
httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

mRetrofit = new Retrofit.Builder()
.client(httpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.build();

mRxjavaService=mRetrofit.create(RxjavaService.class);

}

private static class singletonHolder{
private static final HttpMethods INSTANCE = new HttpMethods();
}

public static HttpMethods getInstance(){
return singletonHolder.INSTANCE;
}

public void getRxjavaData(Subscriber<RetrofitEntity> subscriber,int start,int count){
mRxjavaService.getRxjavaData(start,count)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
}
  • 封装后再mainactivity中的使用
public void getdata2() {
//利用封装后的retrofit来请求
mSubscriber = new Subscriber<RetrofitEntity>() {
@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {

}
@Override
public void onNext(RetrofitEntity retrofitEntity) {
mTextview.setText(retrofitEntity.getSubjects().get(1).getTitle());
}
};
HttpMethods.getInstance().getRxjavaData(mSubscriber,0,10);
}

这次真的结束了,当然,这只是一次简单的解析,想了解更多可以访问:
http://gank.io/post/56e80c2c677659311bed9841