參考文章 : 使用Retrofit和Okhttp實現(xiàn)網(wǎng)絡緩存。無網(wǎng)讀緩存咳焚,有網(wǎng)根據(jù)過期時間重新請求
參考文章 : okhttp3 retrofit2 緩存問題
我是按照上面文章的思路,把緩存加入到自己的項目
- 首先就是思路和基本基本一樣的
然后就是我自己在弄的時候發(fā)現(xiàn)一些問題
Fatal Exception thrown on Scheduler.Worker thread
出現(xiàn)這個問題的主要原因就是Retrofit 和OKHttp 的版本問題
我自己用到的 Retrofit2的版本是2.1.0版本,也就是最新的, OKHttp 3的版本是3.4.0 但是這樣的組合就是出現(xiàn)了問題,
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:54)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5938)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
在網(wǎng)上也看了好多解決辦法,這類的問題發(fā)現(xiàn)的也不多,網(wǎng)上的辦法也不好使,然后就想到了版本問題
我目前有就兩種的解決方案,第一種就是更改對應的版本
我現(xiàn)在使用的版本目前是沒有問題的
//compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
//TODO換這個版本就沒問題 否則會出現(xiàn) 線程調度異常
compile 'com.squareup.okhttp3:okhttp:3.0.1'
然后還有一種解決方案,昨天一不小心就可以了,但是寫Okhttp緩存的時候有一次出現(xiàn)了類似的問題,我先貼出來(我覺得還是版本的問題更大一些)
o.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
就是這樣
- 然后就是我自己的代碼,主要還是參考網(wǎng)絡上很多很多文章
緩存的主要目的還是在沒有網(wǎng)絡的情況不至于一片白花花的view,然后就是不用每次都請求下載數(shù)據(jù)
我們還是用OkHttp緩存
上面文章都很明白我直接上代碼就OK
首先就是需要添加addInterceptor和 addNetworkInterceptor這兩個
這兩個現(xiàn)在要必須都設置才能緩存否則的話不行,其他依賴版本的我也沒有試
先說添加的Interceptor
這個主要是設置有網(wǎng)就去網(wǎng)絡,沒網(wǎng)就讀取本地的緩存
/**
* 設置返回數(shù)據(jù)的 Interceptor 判斷網(wǎng)絡 沒網(wǎng)讀取緩存
*/
public Interceptor getInterceptor(){
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!APPNetWork.isNetworkConnected(App.getIntense())) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
return chain.proceed(request);
}
};
}
另外一個就是NetWorkInterceptor
/**
* 設置連接器 設置緩存
*/
public Interceptor getNetWorkInterceptor (){
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (APPNetWork.isNetworkConnected(App.getIntense())) {
int maxAge = 0 * 60;
// 有網(wǎng)絡時 設置緩存超時時間0個小時
response.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.removeHeader("Pragma")
.build();
} else {
// 無網(wǎng)絡時抖坪,設置超時為1周
int maxStale = 60 * 60 * 24 * 7;
response.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.removeHeader("Pragma")
.build();
}
return response;
}
};
}
最后看一下client設置進去就行了
.addNetworkInterceptor(getNetWorkInterceptor())
.addInterceptor(getInterceptor())
![輸入圖片說明](https://static.oschina.net/uploads/img/201607/12185555_W1X4.png)
這個就是緩存文件