RxHttp-RxJava
Overview
Retrofit 與 RxJava 完美結(jié)合,支持斷點下載,上傳,支持緩存,自定義綁定生命周期
效果圖

image
Download
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.JiangHaiYang01:RxHttp-RxJava:0.0.2'
}
Usage
基礎使用
配置retrifot
rxHttp = RxHttp.Builder()
.baseUrl("https://www.wanandroid.com")
.isLog(true)
.level(HttpLevel.BODY)
.writeTimeout(10)
.readTimeout(10)
.connectTimeout(10)
.build(this)
get 網(wǎng)絡請求
private fun getRequest() {
Log.i(TAG, "get 方法啟動 線程 ${Thread.currentThread().name}")
val data = rxHttp
.create()
.addParameter("k", "java")
.doGet(
parameter = "wxarticle/chapters/json", tClass = TestBean::class.java,
listener = object : OnHttpListener<TestBean>() {
override fun onSuccess(t: TestBean) {
log.text = t.toString()
}
override fun onError(e: Throwable) {
log.text = e.toString()
}
}
)
Log.i(TAG, "收到響應 $data thread ${Thread.currentThread().name}")
}
post網(wǎng)絡請求
private fun postRequest() {
val data = rxHttp
.create()
.addParameter("title", "123456")
.addParameter("author", "123456")
.addParameter("link", "123456")
.doPost("lg/collect/add/json", TestBean::class.java, object : OnHttpListener<TestBean>() {
override fun onSuccess(t: TestBean) {
log.text = t.toString()
}
override fun onError(e: Throwable) {
log.text = e.toString()
}
})
}
說明
create 方法創(chuàng)建一個請求
使用 addParameter 添加請求參數(shù)
使用 addHeard 添加請求頭
使用 bindEvent 綁定生命周期
使用 addFile 添加上傳的文件(在上傳時候使用才有效)
斷點下載
- 啟動下載
rxHttp.create().doDownLoad(info.taskId, info.url, getBasePath(this), info.saveName, this)
接口返回
interface DownLoadProgressListener {
/**
* 下載進度
*
* @param key url
* @param progress 進度
* @param read 讀取
* @param count 總共長度
* @param done 是否完成
*/
fun onUpdate(
key: String,
progress: Int,
read: Long,
count: Long,
done: Boolean
)
}
interface OnDownLoadListener : DownLoadProgressListener {
//等待下載
fun onDownLoadPrepare(key: String)
//進度
fun onDownLoadProgress(key: String, progress: Int)
//下載失敗
fun onDownLoadError(key: String, throwable: Throwable)
//下載成功
fun onDownLoadSuccess(key: String, path: String)
//下載暫停
fun onDownLoadPause(key: String)
//下載取消
fun onDownLoadCancel(key: String)
}
- 取消某一個下載任務
doDownLoadCancel(key: String)
- 暫停某一個下載任務
doDownLoadPause(key: String)
- 取消全部任務
doDownLoadCancelAll
- 暫停全部任務
doDownLoadPauseAll
上傳
- 啟動上傳任務
private fun startUploadSuspend(info: UpLoadInfo) {
rxHttp.create()
.addFile("uploaded_file", File(info.path))
.addHeard("heard", "1")
.addParameter("parameter", "2")
.doUpload(
info.taskId,
"http://t.xinhuo.com/index.php/Api/Pic/uploadPic",
TestBean::class.java,
this
)
}
- 取消某一個上傳任務
doUpLoadCancel(tag:String)
加入其他自定義的解析器
項目本身 加入了解析器
client.addConverterFactory(GsonConverterFactory.create()) // json 解析器
client.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 支持RxJava
如果想支持其他解析器也是可以的
在 build RxHttp 的時候 使用 addBuilderClientListener 添加解析器
eg:
rxHttp = RxHttp.Builder()
.baseUrl("https://www.wanandroid.com")
.isLog(true)
.level(HttpLevel.BODY)
.writeTimeout(10)
.readTimeout(10)
.connectTimeout(10)
.addBuilderClientListener(object : OnBuildClientListener {
override fun addBuildClient(): MutableSet<Any> {
return mutableSetOf(GsonConverterFactory.create(),RxJava2CallAdapterFactory.create())
}
})
.build(this)
是否顯示日志打印 && 日志級別
···
.isLog(true)
.level(HttpLevel.BODY)
···
保存網(wǎng)絡請求的log
有時候 在調(diào)試的時候可能需要將 網(wǎng)絡請求的log 保存到 本地文件,這里也提供接口,開發(fā)者可使用 addLogListener 自行處理log文件
eg:
rxHttp = RxHttp.Builder()
.baseUrl("https://www.wanandroid.com")
.isLog(true)
.level(HttpLevel.BODY)
.writeTimeout(10)
.readTimeout(10)
.addLogListener(this)
.connectTimeout(10)
.build(this)

2020-06-04-15-32-48-1591255968
有時候可能不需要那么多的日志 可以使用 addLogFilter自定添加過濾器
eg:
rxHttp = RxHttp.Builder()
.baseUrl("https://www.wanandroid.com")
.isLog(true)
.level(HttpLevel.BODY)
.writeTimeout(10)
.readTimeout(10)
.addLogFilter(object :OnLogFilterListener{
override fun filter(message: String): Boolean {
if(message.contains("adb")){
return true
}
return false
}
})
.connectTimeout(10)
.build(this)
注意 上傳和下載的日志已經(jīng)過濾了,是不會顯示上傳和下載的日志的,這里主要是防止
@Steam注解失效
網(wǎng)絡cache
在構建 RxHttp 的時候 使用 cacheType 方法,構建緩存策略
提供 4中緩存策略,默認是沒有網(wǎng)絡緩存的
enum class CacheType {
//不加入緩存的邏輯
NONE,
//有網(wǎng)時:每次都請求實時數(shù)據(jù); 無網(wǎng)時:無限時請求有網(wǎng)請求好的數(shù)據(jù);
HAS_NETWORK_NOCACHE_AND_NO_NETWORK_NO_TIME,
//有網(wǎng)時:特定時間之后請求數(shù)據(jù); 無網(wǎng)時:無限時請求有網(wǎng)請求好的數(shù)據(jù);
HAS_NETWORK_CACHE_TIME_AND_NO_NETWORK_NO_TIME,
//有網(wǎng)時:每次都請求實時數(shù)據(jù); 無網(wǎng)時:特定時間之前請求有網(wǎng)請求好的數(shù)據(jù);
HAS_NETWORK_NOCACHE_AND_NO_NETWORK_HAS_TIME,
//有網(wǎng)時:特定時間之后請求數(shù)據(jù); 無網(wǎng)時:特定時間之前請求有網(wǎng)請求好的數(shù)據(jù);
HAS_NETWORK_CACHE_TIME_AND_NO_NETWORK_HAS_TIME,
}
當使用cache 的時候 提供下面api 處理緩存時間等
- cacheNetWorkTimeOut
有網(wǎng)時:特定時間之后請求數(shù)據(jù);(比如:特定時間為20s) 默認20
- cacheNoNetWorkTimeOut
無網(wǎng)時:特定時間之前請求有網(wǎng)請求好的數(shù)據(jù);((比如:特定時間為30天) 默認30 天 單位(秒)
- cacheSize
緩存大小 默認10M
- cachePath
緩存位置 默認沙盒目錄下 cacheHttp 文件夾
Cookie 攔截器
fun addCookieInterceptor(
cookieListener: OnCookieListener,
onCookieInterceptor: OnCookieInterceptor
)
下載安裝包

掃碼下載
License
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.