请问如何让请求在一段时间内只使用缓存的数据
abcdisgreat opened this issue · 13 comments
除了第一次数据,这段时间内即使有网络也从缓存拿数据?
本身就是这样的,你设置个缓存时间,在这段时间内只走缓存
你详细看看README里的使用方法,每一步都要添加
把代码贴出来看看吧
@YuPf1989 没有解决,作者说有这样的配置但我没找到,介绍里的方法在有网时都没能强制从缓存取
创建okhttp部分
`
private RetrofitHelper() {
retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(MyGsonConverterFactory.create())
.client(getOkHttpClient())
.build();
RetrofitCache.getInstance().addRetrofit(retrofit);
}
private OkHttpClient getOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// 200M
int cacheSize = 200 * 1024 * 1024;
File httpcacheFile = new File(mContext.getCacheDir(), "httpcache");
Cache cache = new Cache(httpcacheFile, cacheSize);
builder
.cache(cache)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(new CacheForceInterceptorNoNet())
.addNetworkInterceptor(new CacheInterceptorOnNet())
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
return builder.build();
}`
接口部分
@Cache(time = 2,timeUnit = TimeUnit.MINUTES,forceCacheNoNet = true) @GET("http://47.93.136.56:7011/appInterface/patient/getWikiList.html") Observable<NewsListBean> getWikiList2();
调用部分
` RetrofitHelper.getInstance().retrofit.create(RetrofitService.class).getWikiList2()
.compose(CacheTransformer.emptyTransformer())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new MyObserver() {
@Override
public void onNext(NewsListBean data) {
String content = data.toString();
tvContent.append("content:" + content + "\n");
}
@Override
public void onError(Throwable e) {
super.onError(e);
}
});`
log输出
09-26 17:23:36.551 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200 09-26 17:23:38.436 319-637/? I/BufferQueueProducer: [com.rain.rxjava2demo/com.rain.rxjava2demo.ui.RetrofitCacheActivity](this:0x7f9fb88400,id:17208,api:1,p:16277,c:319) queueBuffer: fps=27.33 dur=2048.96 max=1156.41 min=8.22 09-26 17:23:38.500 16277-16277/com.rain.rxjava2demo W/retrofitcache: java.lang.NoSuchMethodException: toRequest [class [Ljava.lang.Object;] 09-26 17:23:38.555 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200 09-26 17:23:39.495 319-1177/? I/BufferQueueProducer: [com.rain.rxjava2demo/com.rain.rxjava2demo.ui.RetrofitCacheActivity](this:0x7f9fb88400,id:17208,api:1,p:16277,c:319) queueBuffer: fps=51.91 dur=1059.44 max=163.82 min=8.15 09-26 17:23:39.572 16277-16277/com.rain.rxjava2demo W/retrofitcache: java.lang.NoSuchMethodException: toRequest [class [Ljava.lang.Object;] 09-26 17:23:39.629 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200
不知道哪个环节出了问题?
还有必须添加 .compose(CacheTransformer.emptyTransformer())吗?
.compose(CacheTransformer.emptyTransformer()) 这个必须要加,你更新至1.0.9 试试
更新过后试了试,在有网络的情况下,仍旧每次请求都提示from net,另外发现一个有意思的问题,在手机全部断开网络的情况下,并且有之前的缓存,如果超出了缓存时间,进行请求的时候,通过查看okhttp的log,会发现okhttp发起了网络请求,提示from net,之后再多次调用,retrofitcache log输出提示的是from cache,但是查看okhttp的log,仍然是进行了网络连接,状态码200(注意都是在没网的情况下),不知道是怎么一回事,为何没有网络http状态码会是200?是我哪里理解的不到位吗?
调用:RetrofitCache.getInstance().init(Context) 了吗?
调用了,现在在看okhttp缓存这一块,可能哪里理解出现了偏差
okhttp 从缓存拿数据也是200
Hi, @YuPf1989, have you figured out what's wrong?
I am new to this and have a similar situation that it only uses cache when offline.
Later I found that the header of the response contains cache-control directives like this:
no-store, no-cache, must-revalidate
This seems inhibit OkHttpClient from properly caching.
I know we can use interceptor to modify the response, but it seems not affect the OkHttpClient's cache behavior...