Retrofit2.0+RxJava1.0应用

一、简介

        在博客Retrofit2应用 中介绍了,在定义Retrofit网络请求接口时,我们的返回结果是Call<T>,但是在结合RxJava 时,就不能返回一个以OkHttp主类Call为对象的值,而是返回一个可以作为响应式编程RxJava开发库调用的Observable对象,而其他的创建Retrofit对象,接口参数及其注解都不需要发生太大变化,下面学习如何用Retrofit2.0+RxJava1.0 实现网络请求。

二、使用

        1、添加依赖库

/*集成retrofit依赖*/

compile'com.squareup.retrofit2:retrofit:2.2.0'

/*与Rxjava集成时用到*/

compile'com.squareup.retrofit2:adapter-rxjava:2.1.0'

/*需要将对象与json数据转换时用到*/

compile'com.squareup.retrofit2:converter-gson:2.1.0'

/*集成rxjava需要的两个库*/

compile'io.reactivex:rxjava:1.0.14'

compile'io.reactivex:rxandroid:1.0.1'

        2、创建接口

        2.1、GET

@GET(Configs.VERSION_PATH)

//请求方式

Observable<VersionInfo> getVersionInfoRxjava();

        2.2、POST

@FormUrlEncoded//编解码方式

@POST(Configs.ENGLISH_SENTENCE_PATH)

//请求方式

Observable<EnglishSentence> getEnglishSentenceRxjava(

@Field("count")intcount,//参数一

@Field("showapi_appid") String showapi_appid,//参数二

@Field("showapi_sign") String showapi_sign);//参数三

        3、构造Retrofit

OkHttpClient okHttpClient =new OkHttpClient.Builder()

.connectTimeout(5000,TimeUnit.SECONDS)

.readTimeout(5000,TimeUnit.SECONDS)

.build();

okHttpClient.connectTimeoutMillis();

Retrofit retrofit =newRetrofit.Builder()

.baseUrl(Configs.BASE_URL)

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.addConverterFactory(GsonConverterFactory.create())

.client(okHttpClient)

.build();

ApiService apiService= retrofit.create(ApiService.class);

        4、代码使用

        4.1、GET

retrofit.create(ApiService.class)

.getVersionInfoRxjava()

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(newSubscriber() {

@Override

public void onCompleted() {

Log.d(TAG,"onCompleted() called");

}

@Override

public void onError(Throwable e) {

Log.d(TAG,"onError() called with: e = ["+ e +"]");

}

@Override

public void onNext(VersionInfo versionInfo) {

Log.e(TAG,versionInfo.getVersionName() +"-----------");

Log.e(TAG,versionInfo.getVersionCode() +"-----------");

}

});

        4.2、POST

retrofit.create(ApiService.class)

.getEnglishSentenceRxjava(5,Configs.APPID_STRING,Configs.APPSIGN_STRING)

.flatMap(newFunc1<EnglishSentence,Observable<DataBean>>() {

@Override

public Observable<DataBean> call(EnglishSentence englishSentence) {

return Observable.from(englishSentence

.getShowapi_res_body()

.getData());

}})

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(newObserver() {

@Override

public void onCompleted() {

Log.d(TAG,"onCompleted() called");}

@Override

public void onError(Throwable e) {

Log.d(TAG,"onError() called with: e = ["+ e +"]");}

@Override

public void onNext(DataBean dataBean) {

Log.e(TAG,dataBean.getChinese() +"-------------------");

Log.e(TAG,dataBean.getEnglish() +"-------------------");

}});

        5、结果

        5.1、GET

GET

        5.2、POST

POST




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载自:https://xiaobailong24.me/2017/03/18/Android-RxJava2.x...
    Young1657阅读 6,130评论 1 9
  • 我从去年开始使用 RxJava ,到现在一年多了。今年加入了 Flipboard 后,看到 Flipboard 的...
    Jason_andy阅读 10,925评论 7 62
  • 前言RxJava和Retrofit也火了一段时间了,不过最近一直在学习ReactNative和Node相关的姿势,...
    AFinalStone阅读 3,596评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,901评论 25 709
  • 文章 濯堂 文章,简单地说就是说几句话而已,就是一句真话怎么说的问题。没有那么复杂,也不必搞得那么复杂。 ...
    濯堂阅读 2,895评论 3 3