RxJava2和Retrofit封装的RetrofitClient2终于来了!

版权声明:本文为博主原创文章,请尊重原创,转载请标明地址。有问题请关注 [开发者技术前线] 咨询 https://blog.csdn.net/sk719887916/article/details/75223423

Tamic : http://blog.csdn.net/sk719887916/article/details/51958010

Tamic 只出高质量干货

RxJava1和Retrofit2组合的好用的RetrofitClient笔者去年写了一篇,后续有朋友私信要求升级第二版本,本打算 不维护了,但是很多人要用Rxjava2,所以便进行了支持Rxjava2的升级,升级过程中遇到了一些麻烦,经过一段时间的测试后,写了这篇文章,如果想要更要完整的功能请参考Novate.

RxJava1:

RxJava+retrofit的请参考:
Android基于Retrofit2.0+RxJava 封装的超好用的RetrofitClient工具类

写在最前:

本次封装的优势:

  • 避免重复创建Retrofit实列.

  • 调用方便简洁.

  • 无需重复设置属性的步骤.

  • 可固定配置 Host 也可动态配置Url、请求头、参数等.

  • 支持文件下载和上传.

  • 支持json,表单形式提交.

  • 支持扩展APIService

  • 统一处理无网络情况,和支持加载进度

  • 支持缓存机制,cookie同步

  • 优化取消功能

本次我使用的是最新Rxjava2.1, Retrofit 2.3, okhttp3.8 进行开发的。其他版本差异请看官方说明。

实现的过程可以看第一篇详细介绍!这里不在累赘,但值得注意的是 RxJava2最大的优势就是增加了被压,和 Flowable,收回Obserable权限等,除了包名发生变化,一些类名也进行了改变,比如:Func就变成了Funtion, Action变为Customer, Transformer 变为 FlowableTransformer 还有支持了Map操作符升级的数组泛型支持的mapArray等。

其他优势可以去RxAava 的GItHub阅读:
https://github.com/ReactiveX/RxJava

Retrofit在最新的版本中也发生了变化,@PartMap将不支持注解Key指,提供了另一种方式@partList等, 这种方式可以让多文件上传变得更加灵活:

public interface UpLoadApiService {

    @GET("service/upload")
    Flowable<BaseResponse<IpResult>> upload(
     @PartList   List<RequstBody> list);
}

使用:

项目地址:https://github.com/Tamicer/RetrofitClient/commits/2.x

API姿势

Android基于Retrofit2.0+RxJava 封装的超好用的RetrofitClient工具类

GET

 RetrofitClient.getInstance(context)
                    .createBaseApi()
                    .get("url", maps)
                    .subscribe(new BaseSubscriber<T>(context) {});

POST

 RetrofitClient.getInstance(MainActivity.this)
                    .createBaseApi()
                    .post("url", maps)
                    .subscribe( new BaseSubscriber<T>(context) {});

JSON

   RequestBody jsonbody = 
               RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(user));

   RetrofitClient.getInstance(MainActivity.this)
                  .createBaseApi()
                  .json("url", jsonBody)
                  .subscribe( new BaseSubscriber<T>(context) {});

UpLoad

    RequestBody requestFile =
                    RequestBody.create(MediaType.parse("image/jpg"), new File(mPath));

    RetrofitClient.getInstance(MainActivity.this)
                   .createBaseApi()
                   .upload(url, requestFile)
                   .subscribe(new BaseSubscriber<T>(context) {
                    });

Download

  RetrofitClient.getInstance(MainActivity.this)
  .createBaseApi()
  .download(url1, new CallBack() {


            );

Execute you ApiService

定义MyApiService

public interface MyApiService {

    @GET("service/getIpInfo.php")
    Flowable<BaseResponse<IpResult>> getData(@Query("ip") String ip);
}

Create ApiService

            //create  you APiService
            MyApiService service = RetrofitClient.getInstance().create(MyApiService.class);

Call

            // execute and add observable
            RetrofitClient.getInstance(MainActivity.this)
                    .switchSchedulersMain(service.getData("21.22.11.33"))
                    .subscribe(new BaseSubscriber<BaseResponse<IpResult>>(MainActivity.this) {

                        });

# More Rxjava

如果想操作RxJAVA更多操作符:


  RetrofitClient.getInstance(Context)
                        .createBaseApi()
                        .get("service/getIpInfo.php", maps)
                        .compose(new FlowableTransformer() {
                            @Override
                            public Publisher apply(@NonNull Flowable upstream) {
                                return null;
                            }
                        })
                        .map(new Function() {
                            @Override
                            public Object apply(@NonNull Object o) throws Exception {
                                return null;
                            }
                        })
                        .xxxxx()//更多
                        .subscribe(new BaseSubscriber<BaseResponse<IpResult>>() {

                            })

取消:

Flowable flowable =   RetrofitClient.getInstance()
                    .createBaseApi()
                    .get()
                   .ubscription()
                   .unsubscribe();

Rxjava结合Retrofit,如何优雅的取消请求!
优雅的取消请看:http://blog.csdn.net/sk719887916/article/details/54575137

不华丽结束

同样的本次简单封装适合一些入门了rxjava的朋友,上传进度啥可以去看我另一项目,这个版本设计也不未必是完美的,只是用来学习设计思想,喜欢的朋友可以直接源码自己增加想要的功能,如果想学习更多的姿势朋友推荐一款本人写的框架:Novate.

Novate
https://github.com/Tamicer/Novate

作者 Tamic
原文:http://blog.csdn.net/sk719887916/article/details/51958010

猜你喜欢

转载自blog.csdn.net/sk719887916/article/details/75223423