RxJava+Retrofit网络加载库二次封装-RxHttpUtils

RxHttpUtils是RxJava+Retrofit网络加载库二次封装,包含网络加载动画、activity销毁自动取消请求、网络缓存、公共参数、RSA+AES加密等
GitHub仓库地址

引入

gradle

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

implementation 'com.github.DL-ZhangTeng:RxHttpUtils:1.4.0'
    //库所使用的三方
    implementation 'androidx.lifecycle:lifecycle-common:2.4.0'
    implementation 'androidx.lifecycle:lifecycle-runtime:2.4.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.21'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.8.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2'
    //noinspection GradleDynamicVersion
    implementation 'com.github.DL-ZhangTeng.BaseLibrary:utils:1.4.+'

属性

属性名描述
setBaseUrlConfigGlobalHttpUtils()全局的BaseUrl;ConfigSingleInstance()单独设置BaseUrl
addCallAdapterFactory设置CallAdapter.Factory,默认RxJavaCallAdapterFactory.create()
addConverterFactory设置Converter.Factory,默认GsonConverterFactory.create()
setDns自定义域名解析
setCache开启缓存策略
addHeader全局的单个请求头信息
setHeaders全局的请求头信息,设置静态请求头:更新请求头时不需要重新设置,对Map元素进行移除添加即可;设置动态请求头:如token等需要根据登录状态实时变化的请求头参数,最小支持api 24
setSign全局验签,appKey与后端匹配即可,具体规则参考:https://blog.csdn.net/duoluo9/article/details/105214983
setEnAndDecryption全局加解密(AES+RSA)。1、公钥请求路径HttpUrl.get(BuildConfig.HOST + “/getPublicKey”);2、公钥响应结果{“result”: {“publicKey”: “”},“message”: “查询成功!”,“status”: 100}
setCookie全局持久话cookie,保存本地每次都会携带在header中
setSslSocketFactory全局ssl证书认证。1、信任所有证书,不安全有风险,setSslSocketFactory();2、使用预埋证书,校验服务端证书(自签名证书),setSslSocketFactory(getAssets().open(“your.cer”));3、使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书),setSslSocketFactory(getAssets().open(“your.bks”), “123456”, getAssets().open(“your.cer”))
setReadTimeOut全局超时配置
setWriteTimeOut全局超时配置
setConnectionTimeOut全局超时配置
setLog全局是否打开请求log日志

使用

 public class MainApplication extends Application {
     private static MainApplication mainApplication;
     private final Map<String, Object> headersMap = new HashMap<>();

     public static MainApplication getInstance() {
         return mainApplication;
     }

    @Override
    public void onCreate() {
        super.onCreate();
        HttpUtils.init(this);
        HttpUtils.getInstance()
                .ConfigGlobalHttpUtils()
                //全局的BaseUrl
                .setBaseUrl("http://**/")
                //设置CallAdapter.Factory,默认RxJavaCallAdapterFactory.create()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //设置Converter.Factory,默认GsonConverterFactory.create()
                .addConverterFactory(GsonConverterFactory.create())
                //设置自定义域名解析
                .setDns(HttpDns.getInstance())
                //开启缓存策略
                .setCache(true)
                //全局的单个请求头信息
                .addHeader("Authorization", "Bearer ")
                //全局的静态请求头信息
                //.setHeaders(headersMap)
                //全局的请求头信息
                //.setHeaders(headersMap, headers -> {
                //  if (headers == null) {
                //      headers = new HashMap<>();
                //  }
                //  boolean isLogin = BuildConfig.DEBUG;
                //  if (isLogin) {
                //      headers.put("Authorization", "Bearer " + "token");
                //  } else {
                //      headers.remove("Authorization");
                //  }
                //  return headers;
                //})
                //全局的动态请求头信息
                .setHeaders(headers -> {
                    if (headers == null) {
                        headers = new HashMap<>();
                    }
                    headers.put("version", BuildConfig.VERSION_CODE);
                    headers.put("os", "android");

                    boolean isLogin = BuildConfig.DEBUG;
                    if (isLogin) {
                        headers.put("Authorization", "Bearer " + "token");
                    } else {
                        headers.remove("Authorization");
                    }
                    return headers;
                })
                //全局持久话cookie,保存本地每次都会携带在header中
                .setCookie(false)
                //全局ssl证书认证
                //信任所有证书,不安全有风险
                .setSslSocketFactory()
                //使用预埋证书,校验服务端证书(自签名证书)
                //.setSslSocketFactory(getAssets().open("your.cer"))
                //使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书)
                //.setSslSocketFactory(getAssets().open("your.bks"), "123456", getAssets().open("your.cer"))
                //全局超时配置
                .setReadTimeOut(10)
                //全局超时配置
                .setWriteTimeOut(10)
                //全局超时配置
                .setConnectionTimeOut(10)
                //全局是否打开请求log日志
                .setLog(true);
    }
}
//使用生命周期监听自动取消请求、加载中动画自动处理(LifecycleObservableTransformer、ProgressDialogObservableTransformer)
 HttpUtils.getInstance()
                .ConfigGlobalHttpUtils()
                .createService(ApiService.class)
                .loginPwd("admin", "admin")
                .compose(new LifecycleObservableTransformer<>(MainActivity.this))
                .compose(new ProgressDialogObservableTransformer<>(mProgressDialog))
                .subscribe(new BaseObserver<BaseResponse<LoginBean>>() {
                    @Override
                    public void doOnSubscribe(Disposable d) {

                    }

                    @Override
                    public void doOnError(IException iException) {

                    }

                    @Override
                    public void doOnNext(BaseResponse<LoginBean> loginBeanBaseResponse) {

                    }

                    @Override
                    public void doOnCompleted() {

                    }
                });
                
//使用生命周期监听自动取消请求、加载中动画自动处理(CommonObserver方案)
//        HttpUtils.getInstance()
//                .ConfigSingleInstance()
//                .setBaseUrl("https://**/")
//                .createService(ApiService.class)
//                .loginPwd("admin", "admin")
//                .subscribeOn(Schedulers.io())
//                .doOnSubscribe(disposable -> mProgressDialog.show())
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CommonObserver<BaseResponse<LoginBean>>(mProgressDialog) {
//                    @Override
//                    protected void onFailure(IException iException) {
//
//                    }
//
//                    @Override
//                    protected void onSuccess(BaseResponse<LoginBean> loginBeanBaseResponse) {
//                        ToastUtilsKt.showShortToast(MainActivity.this, loginBeanBaseResponse.getMsg());
//                    }
//                });

//手动取消网络请求
//        HttpUtils.getInstance().cancelSingleRequest(this);
//        HttpUtils.getInstance().cancelSingleRequest(Disposable);
//        HttpUtils.getInstance().cancelAllRequest();

混淆

-keep public class com.zhangteng.**.*{ *; }

历史版本

版本更新更新时间
v1.4.0使用rxJava时向外抛出IException,便于调试2022/8/22 at 10:46
v1.3.0增加addConverterFactory&addCallAdapterFactory&addHeader2022/7/9 at 13:07
v1.2.2使用util库中的IException解决循环依赖&异常处理放入子类实现,方便调试异常2022/7/4 at 17:50
v1.2.1增加动态请求头添加方法&自定义域名解析2022/6/25 at 16:46
v1.2.0使用base库utils2022/1/21 at 20:14
v1.1.9ConcurrentModificationException报错2022/1/2 at 21:28
v1.1.8下载文件配置2021/12/21 at 23:42
v1.1.7文件上传支持自定义字段名2021/10/28 at 17:06
v1.1.6请求取消方案修改:1、Lifecycle生命周期监听自动取消请求增加ObservableTransformer方案;2、请求结束自动从集合移除;3、页面销毁自动移除所有Tag对应的请求2021/10/9 at 15:38
v1.1.51.增加Lifecycle生命周期监听自动取消请求;2.增加通过tag取消请求2021/9/22 at 16:43
v1.1.41.createService时全部使用RetrofitServiceProxyHandler;2.上传下载单例bug2021/7/15 at 15:36
v1.1.3单接口配置与全局配置同步方法&支持UnitTest2021/7/12 at 17:45
v1.1.2增加Service的lru缓存2021/5/10 at 16:25
v1.1.1加解密失败构建响应bug2020/12/14 at 18:04
v1.1.0迁移到androidx2020/7/22 at 13:40
v1.0.3全局拦截器失效bug2020/6/27 at 12:22
v1.0.2增加权限,修改模块名为小写2020/6/11 at 9:45
v1.0.1包结构调整2020/6/11 at 9:36
v1.0.0初版2020/6/3 at 17:13

赞赏

如果您喜欢RxHttpUtils,或感觉RxHttpUtils帮助到了您,可以点右上角“Star”支持一下,您的支持就是我的动力,谢谢

联系我

邮箱:763263311@qq.com/ztxiaoran@foxmail.com

License

Copyright © [2020] [Swing]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐