Skip to content

senierr/RxHttp

Repository files navigation

RxHttp

目前支持

普通get, post, put, delete, head, options, patch请求
自定义请求方法
自定义基础请求地址、请求参数、请求头
自定义请求参数、请求头、请求体
全局任意请求进度监听
多种HTTPS验证
可扩展Cookie管理
多级别日志打印
可扩展数据解析
简洁的链式调用
同步支持RxJava2

更新日志

1.仓库导入

implementation 'com.senierr.http:rxhttp:2.1.2'

RxHttp内部关联依赖:

implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.40'
implementation 'com.squareup.okhttp3:okhttp:3.12.3'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'

2.权限

<uses-permission android:name="android.permission.INTERNET"/>

// 文件上传下载需要以下权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3.实例化

val rxHttp = RxHttp.Builder()
        .debug(...)                 // 开启Debug模式
        .baseUrl(...)               // 设置基础请求地址
        .addBaseHeader(...)         // 增加单个基础头
        .addBaseHeaders(...)        // 增加多个基础头
        .addBaseUrlParam(...)       // 增加单个基础URL参数
        .addBaseUrlParams(...)      // 增加多个基础URL参数
        .connectTimeout(...)        // 设置连接超时(ms)
        .readTimeout(...)           // 设置读超时(ms)
        .writeTimeout(...)          // 设置写超时(ms)
        .hostnameVerifier(...)      // 设置域名校验规则
        .sslFactory(...)            // 设置SSL验证
        .cookieJar(...)             // 设置Cookie管理
        .addInterceptor(...)        // 增加拦截器
        .addNetworkInterceptor(...) // 增加网络层拦截器
        .build()

4.构建请求

// 通过RxHttp实例发起请求
rxHttp.get(...)  // 支持get、post、head、delete、put、options、trace、method(自定义请求)
        .setBaseUrl(...)                // 设置基础请求地址(只作用于当前请求,不会改变公共设置)
        .ignoreBaseUrl()                // 忽略基础请求地址(只作用于当前请求,不会改变公共设置)
        .ignoreBaseUrlParams()          // 忽略基础请求参数(只作用于当前请求,不会改变公共设置)
        .ignoreBaseHeaders()            // 忽略基础请求头(只作用于当前请求,不会改变公共设置)
        .addHeader(...)                 // 增加单个头
        .addHeaders(...)                // 增加多个头
        .addUrlParam(...)               // 增加单个URL参数
        .addUrlParams(...)              // 增加多个URL参数
        .addRequestParam(...)           // 增加单个表单参数
        .addRequestStringParams(...)    // 增加多个字符串表单参数
        .addRequestFileParams(...)      // 增加多个文件表单参数
        .requestBody4JSon(...)          // 设置Json请求体
        .requestBody4Text(...)          // 设置Text请求体
        .requestBody4Xml(...)           // 设置XML请求体
        .requestBody4Byte(...)          // 设置Byte请求体
        .requestBody4File(...)          // 设置File请求体
        .requestBody(...)               // 自定义请求体
        .isMultipart(...)               // 是否分片表单
        .uploadTag(...)                 // 开启上传进度广播
        .downloadTag(...)               // 开启下载进度广播
        .toObservable(...)              // 转为Observable

5.数据解析

在发起请求时,必须设置Converter,以便RxHttp能正确解析数据。

RxHttp内置了两种Converter

  • StringConverter: 返回String
  • FileConverter: 下载文件并返回File

当然,您也可以实现Converter<T>接口,自定义数据解析规则:

public interface Converter<T> {
    @NonNull T convertResponse(@NonNull Response response) throws Throwable;
}

6.结果返回

返回结果的类型为Observable<T>,其中泛型<T>即为解析的结果类型。

7.进度监听

RxHttp上传进度监听下载进度监听进行了剥离,并使其适用于任意请求

在发起请求时,您可以设置:

uploadTag(tag: String)      // 开启上传进度广播
downloadTag(tag: String)    // 开启下载进度广播

您可以在任意位置监听进度:

ProgressBus.toObservable(tag)
        .subscribe {
            ...
        }

8.Cookie

RxHttp提供以下方式持久化管理Cookie

-- SPCookieStore        // SharedPreferences
-- MemoryCookieStore    // 内存

8.1.配置

// 1. 实例化
SPCookieStore cookieStore = new SPCookieStore(context);
// 2. 配置
RxHttp.Builder()
        ...
        .cookieJar(CookieJarImpl(cookieStore))
        .build();

8.2.接口说明

// 是否过期
boolean isExpired(Cookie cookie);

// 保存多个Cookie
void saveCookies(HttpUrl url, List<Cookie> cookies);

// 保存单个Cookie
void saveCookie(HttpUrl url, Cookie cookie);

// 获取URL对应所有Cookie
List<Cookie> getCookies(HttpUrl url);

// 获取所有Cookie
List<Cookie> getAllCookie();

// 移除单个URL对应Cookie
void removeCookie(HttpUrl url, Cookie cookie);

// 移除URL对应所有Cookie
void removeCookies(HttpUrl url);

// 移除所有Cookie
void clear();

8.3.自定义

通过实现CookieStore接口,自定义Cookie管理方式。

9.HTTPS

// 1. 实例化
SSLFactory sslFactory = new SSLFactory(...);
// 2. 配置
new RxHttp.Builder()
        .sslSocketFactory(sslFactory.getsSLSocketFactory(),
                          sslFactory.getTrustManager())
        .build();

// 默认信任所有证书
public SSLFactory()
// 单向认证
public SSLFactory(X509TrustManager trustManager)
// 单向认证
public SSLFactory(InputStream... certificates)
// 双向认证
public SSLFactory(InputStream bksFile, String password, InputStream... certificates)
// 双向认证
public SSLFactory(InputStream bksFile, String password, X509TrustManager trustManager)

10.R8/ProGuard

# OkHttp3
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *;}
-dontwarn okio.**

#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}

#okio
-dontwarn okio.**
-keep class okio.**{*;}

License

Copyright 2018 senierr

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

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.

About

简洁、易用、可扩展的网络请求框架

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages