基於OkHttp Retrofit RxJava 多執行緒下載。請求、快取、自動更新.限制佇列數.封裝庫
阿新 • • 發佈:2019-01-01
XDownload介紹
本庫封裝基於Okhttp3,Retrofit2,RxJava2.0,Greendao3.2
ps : 當然當然,都封裝好了,你也可以無視
GitHub地址
如果你覺得好用,對你有幫助,請給個star
介面使用360市場的如果有侵權,聯絡本人刪除,謝謝
—— [ Demo地址,點選開啟 ]
效果圖
主要功能
- 自動更新,酷炫的水波紋
- 網路請求,支援自動ProgressDialog
- 網路請求,自定義是否快取,無論任何方式
- 網路請求,自定義快取時間,無網路快取時間
- 佇列下載,可限制佇列最大數。
- 斷點下載,APP強制關閉,依舊不影響
- 網路請求快取,可快取String,Gson
- 程式碼全註釋,方便您的閱讀
如何使用
在當前工程中引入
compile ‘com.github.2745329043:XDownloadLibrary:1.0.8’
最好直接參考demo
https://github.com/2745329043/XDownloadDemo
Application初始化工作
//初始化網路請求
RetrofitClient.init(this)
.setDebug(true) //是否輸出除錯日誌
.setBaseUrl(IConstantPool.sCommonUrl)
.setApplictionId(BuildConfig.APPLICATION_ID)
/** 以下都是按需設定 */
.setDownloadsQueueCount(5 ) //下載最大數量
.setDbName("net_buffer_db") //資料庫表名
.setConnectionTimeout(6) //普通請求連線超時
.setReadTimeout(6) //普通請求讀取超時
.setDownConnectionTime(6) //下載連線超時 6秒
.setNetBufferTime(60) //有網路的情況下快取 60 秒
.setNoNetBufferTime(24 * 60 * 60 * 7) //無網路的時候,快取
/** 頭部引數 */
.setNetRequestParamsListener(new NetRequestParamsListener() {
@Override
public HashMap<String, String> getHeaderParams() {
//需要請傳,不需要返回 null
// return null;
HashMap<String, String> map = new HashMap<>();
map.put("userId","2745329043");
return map;
}
})
/** 設定完,記得Buid */
.build();
先來效果圖
網路請求
> **定義 HttpService 介面**
public interface HttpService {
/* 獲取下載列表 post */
@POST(IConstantPool.DOWNLOAD_URL)
Observable<DownListBean> getDownloadList(
@Query("page") int page,
@Query("type") int type
);
/* 普通請求 Get -> 獲取 String 版本*/
@GET(IConstantPool.REQUEST_LIST_URL)
Observable<ResponseBody> requestList();
/* 普通請求 Get -> 獲取 GSON版本 版本 -> 這奇葩結構 是List<XXX> 的這種*/
@GET(IConstantPool.REQUEST_LIST_URL)
Observable<List<BriefListBean>> requestList_GSON();
}
普通String請求
RetrofitClient.getService(HttpService.class)
.requestList()
.compose(new ApplySchedulers<ResponseBody>())
.subscribe(new NetProgressSubscriber<>(RequestFragment.this, NetDialogConfig.NORMAL_LOADING, new SimpleNetResponseListener<ResponseBody>() {
@Override
public void onSucceed(ResponseBody body, String s) {
try {
ToastUtils.getInstance().toast(body.string());
} catch (IOException e) {
e.printStackTrace();
}
}
}));
普通Gson請求
RetrofitClient.getService(HttpService.class)
.requestList_GSON()
.compose(new ApplySchedulers<List<BriefListBean>>())
.subscribe(new NetProgressSubscriber<>(RequestFragment.this, NetDialogConfig.NORMAL_LOADING, new SimpleNetResponseListener<List<BriefListBean>>() {
@Override
public void onSucceed(List<BriefListBean> bean, String s) {
ToastUtils.getInstance().toast("拿到好多資料:" + bean.size());
}
}));
網路請求過程中,是否顯示Dialog
引數 | 說明 |
---|---|
NetDialogConfig.UN_LOADING | 不顯示 |
NetDialogConfig.NORMAL_LOADING | 顯示,點選Dialog外,銷燬並關閉佇列 |
NetDialogConfig.FORBID_LOADING | 顯示,不可取消 |
#
網路請求過程中,是否快取資料
引數 | 說明 |
---|---|
NetBufferConfig.NORMAL_BUFFER | 快取 |
NetBufferConfig.UN_BUFFER | 不快取 |
快取使用案例
自定義快取時間使用案例
String快取處理
因為ResponseBody.string() 方法 機制問題。所以框架內。String快取需要
//需要自己手動儲存
BufferDbUtil.getInstance().updateResulteBy(IConstantPool.REQUEST_LIST_URL,result);
下載功能
//下載管理 - 記得 ondestory
private RetrofitDownloadManager mRetrofitDownloadManager;
//使用 RetrofitDownloadManager 建立下載物件,內部有斷點功能
mDownInfo = mRetrofitDownloadManager.createDownInfo("http://xxx.apk");
//點選按鈕,開始更新
mRetrofitDownloadManager.down(mDownInfo);
就這麼簡單,要記得釋放資源,當然你不做也可以
@Override
public void onDestroy() {
super.onDestroy();
mRetrofitDownloadManager.destory();
}