RXjava+Retrofit 史上最精简封装(值得拥有)

3,939 阅读2分钟

RxHttpUtils

继上次SuperTextView之后的又一次封装

Rxjava+Retrofit封装,便捷使用

编写原由:
项目用到Rxjava+Retrofit的使用,总是感觉封装的不到位,网上也有很多类此的封装,找来找去没有一款适合自己的,无奈之下只能自己动手封装一个使用起来超级简单的网络框架,个人感觉装装的还是挺不错的,相比网络上其他封装简单了不少,使用起来也很方便,源码很少可以随意修改源码达到自己需要的效果,废话不多说了,请看怎么使用!

源码github地址

添加Gradle依赖

先在项目根目录的 build.gradle 的 repositories 添加:

 allprojects {
     repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

然后在dependencies添加:

    dependencies {
    ...
    compile 'com.github.lygttpod:RxHttpUtils:1.0.1'
    }

使用说明

1、使用前自己的application类必须继承BaseApplication

2、自己定义的实体类需要继承BaseResponse基类

3、示例

        Map<String ,Object> map = new TreeMap<>();
        map.put("version","8.8");
        map.put("phoneType","android");

        RxHttpUtils                
                    .getInstance()                              
                    .addHeader(map)                             
                    .createApi(ApiService.class)                
                    .getBanner()                                               
                    .compose(RxHelper.<Banner>io_main())        
                    .subscribe(new CommonSubscriber<Banner>() {                                   
                          @Override
                           protected void onError(String errorMsg) {
                          }
                          @Override
                          protected void onSuccess(Banner banner) {
                                Toast
                                .makeText(MainActivity.this
                                ,banner.getBanners().get(0).getTitle()
                                ,Toast.LENGTH_LONG)
                                 .show();
                          }
                    });

4、参数说明

            1、getInstance(your_base_url)
            可以动态修改baseUrl
            2、addHeader(map)
            动态设置请求头《可以不添加》
            3、ApiService.class
            是自己定义的
            4、getBanner()
            需要需不需要添加参数根据自己的定义设置
            5、io_main(loadingDialog)
            可以设置自己请求开始的loading对话框
            6new CommonSubscriber<Banner>(loadingDialog)
            需要显示loading对话框的话这里需要传入自己的Dialog

5、注意事项:适合请求结果是以下情况的(当然用户可以根据自己的实际需求稍微修改一下代码就能满足自己的需求)

    code为错误状态码,为0时表示无错误; msg为错误描述信息

    注意:请求成功时,msg字段可有可无。

            {
            code: 0/400/401...,
            msg: 错误描述...,
            ...
            ...
            ...
            }
    如果你的服务器返回不是以上格式不要惊慌,下载源码,源码其实很简单,自己重写一个BaseResponse基类,根据自己需求处理,
    修改一下BaseSubscriber和ISubscriber中泛型继承的类就行了

6、后面会陆续完成上传和下载的封装,敬请期待...

意见反馈

如果遇到问题或者好的建议,请反馈到我的邮箱:lygttpod@163.com 或者lygttpod@gmail.com

如果觉得对你有用的话,点一下右上的星星赞一下吧!

传送门

License

     Copyright 2016 Allen

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

      [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
limitations under the License.*斜体*