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

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);
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用:

项目地址: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) {});

   
   
  • 1
  • 2
  • 3
  • 4
  • 5

POST

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

   
   
  • 1
  • 2
  • 3
  • 4
  • 5

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) {});

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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) {
                    });

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Download

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


            );

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Execute you ApiService

定义MyApiService

public interface MyApiService {

    @GET("service/getIpInfo.php")
    Flowable<BaseResponse<IpResult>> getData(@Query("ip") String ip);
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5

Create ApiService

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

   
   
  • 1
  • 2
  • 3

Call

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

                        });

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

# 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>>() {

                            })

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

取消:

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

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值