美文网首页
<转>RxJava+Retrofit+OkHttp 懒人方式使

<转>RxJava+Retrofit+OkHttp 懒人方式使

作者: Vurtex | 来源:发表于2017-08-30 19:34 被阅读0次

背景

之前学习完Retrofit+Rxjava之后写了一篇关于封装的博客,发出后受到大家的关注以及使用,由于不断的完善之前的项目,所以决定把最新的项目封装过程讲解出来,供大家查看!

原博客地址:Rxjava+ReTrofit+okHttp深入浅出-终极封装

懒人简单的使用方式

为什么称为懒人,因为你什么都不用做,直接按照一般案例写rx和retrofit的###使用

20161024093019124.gif
  • 引入需要的包
 /*rx-android-java*/
    compile 'io.reactivex:rxjava:+'
    compile 'com.squareup.retrofit:adapter-rxjava:+'
    compile 'com.trello:rxlifecycle:+'
    compile 'com.trello:rxlifecycle-components:+'
    /*rotrofit*/
    compile 'com.squareup.retrofit2:retrofit:+'
    compile 'com.squareup.retrofit2:converter-gson:+'
    compile 'com.squareup.retrofit2:adapter-rxjava:+'
    compile 'com.google.code.gson:gson:+'
  • 创建一个service定义请求的接口
/**
 * service统一接口数据
 * Created by WZG on 2016/7/16.
 */
public interface HttpService {
    @POST("AppFiftyToneGraph/videoLink")
    Observable<RetrofitEntity> getAllVedioBy(@Body boolean once_no);
}
  • 创建一个retrofit对象
//手动创建一个OkHttpClient并设置超时时间
        okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(5, TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(HttpManager.BASE_URL)
                .build();
  • http请求处理
        //加载框
        final ProgressDialog pd = new ProgressDialog(this);

        HttpService apiService = retrofit.create(HttpService.class);
        Observable<RetrofitEntity> observable = apiService.getAllVedioBy(true);
        observable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                   new Subscriber<RetrofitEntity>() {
                   @Override
                   public void onCompleted() {
                   if (pd != null && pd.isShowing()) {
                             pd.dismiss();
                      }
                   }

                   @Override
                   public void onError(Throwable e) {
                       if (pd != null && pd.isShowing()) {
                              pd.dismiss();
                          }
                       }

                   @Override
                   public void onNext(RetrofitEntity retrofitEntity) {
                        tvMsg.setText("无封装:\n" + retrofitEntity.getData().toString());
                   }

                   @Override
                   public void onStart() {
                         super.onStart();
                         pd.show();
                   }
                }
       );

源码

源码

终极封装专栏

RxJava+Retrofit+OkHttp深入浅出-终极封装专栏

相关文章

  • <转>RxJava+Retrofit+OkHttp 懒人方式使

    背景 之前学习完Retrofit+Rxjava之后写了一篇关于封装的博客,发出后受到大家的关注以及使用,由于不断的...

  • retrofit

    Retrofit+Rxjava+okhttp 懒人方式使用一 Retrofit+Rxjava+okhttp 懒人方...

  • 使转

    古人一点一画,皆使锋转笔以成之,非至起止掣曳之处乃用使转。纵横者,无处不达之谓也。盘纡跳荡,草势也;古人一牵一连,...

  • 使转

    使,谓纵横牵掣之类是也。'掣'以前音chì,'牵掣'就是牵连、牵拉。'纵横'就是竖笔和横笔。'纵横牵掣'就是竖笔和...

  • 实力干货|办公职场人士常见格式转换问题,这里全都有!

    【懒人目录】 一、office文档互转 Word转PPT Word转Excel3.Excel转Word4.PPT转...

  • 网络库浅析

    网络方案说明 Android时下最流行的网络方案——“RxJava+Retrofit+OKHttp”,我们先通过官...

  • 封装RxJava+Retrofit+OkHttp系列——(二)自

    封装RxJava+Retrofit+OkHttp系列——(一)自动处理网络请求出错 完整项目Github地址:戳这...

  • 懒人祥子寻活记(转)

    祥子从小失去了父母,是街坊们养大的,东家一顿西家一宿,街坊们一直宠着他,祥子也就变得又懒又馋了。 转眼间祥子长成了...

  • <转>RxJava+Retrofit+OkHttp深入浅出-终极

    封装成果 封装完以后,具有如下功能: 效果: 具体使用: 封装后http请求代码如下 是不是很简单?你可能说这还简...

  • <转>RxJava+Retrofit+OkHttp深入浅出-终极

    背景 在前面Rxjava+ReTrofit+okHttp深入浅出-终极封装专栏我们已经全面的封装了一套可以投入实战...

网友评论

      本文标题: <转>RxJava+Retrofit+OkHttp 懒人方式使

      本文链接:https://www.haomeiwen.com/subject/flvxjxtx.html