我想实现缓存,即使没有互联网连接(离线),但仍然没有成功,已经看了很多例子,但仍然没有运气
//FeedInterceptor Class
public static Interceptor getOfflineInterceptor(final Context context){
Interceptor interceptor = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!isNetworkAvailable(context)) {
request = request.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached")
.build();
}
return chain.proceed(request);
}
};
return interceptor;
}
//OnCreate Activity
client = new OkHttpClient.Builder()
.addNetworkInterceptor(FeedInterceptor.getOnlineInterceptor(this))
.addInterceptor(FeedInterceptor.getOfflineInterceptor(this))
.cache(cache)
.build();
//After build Request
Response response = client.newCall(request).execute();
return response.body().string();如果脱机,则返回为空字符串。我是不是遗漏了什么,或者说错了什么?
发布于 2018-02-27 22:21:52
为了能够重用缓存的响应,有必要通过提供一些头部(例如,Cache-Control: public或Cache-Control: max-age=3600)来声明响应本身是“可缓存的”。你有没有检查你的响应是否包含这样的报头?
另外,考虑使用内置常量CacheControl.FORCE_CACHE作为cacheControl setter的值。
最后,请注意(引用自OkHttp documentation):
注意:如果您使用
FORCE_CACHE并且响应需要网络,则OkHttp将返回504 Unsatisfiable Request响应
编辑:
这里有一个完整的例子。
final CacheControl cacheControl = new CacheControl.Builder()
.maxAge(60, TimeUnit.MINUTES)
.build();
// Interceptor to ask for cached only responses
Interceptor cacheResponseInterceptor = chain -> {
Response response = chain.proceed(chain.request());
return response.newBuilder()
.header("Cache-Control", cacheControl.toString())
.build();
};
// Interceptor to cache responses
Interceptor cacheRequestInterceptor = chain -> {
Request request = chain.request();
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
return chain.proceed(request);
};
// Create a directory to cache responses. Max size = 10 MiB
Cache cache = new Cache(new File("okhttp.cache"), 10 * 1024 * 1024);
/*
* Let's create the client. At the beginning the cache will be empty, so we will
* add the interceptor for the request only after, for the 2nd call
*/
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.addNetworkInterceptor(cacheResponseInterceptor)
.build();
// Let's do the call
Request request = new Request.Builder()
.url("http://httpbin.org/get")
.get()
.build();
Response response = client.newCall(request).execute();
response.close();
// Let's add the interceptor for the request
client = client.newBuilder()
.addInterceptor(cacheRequestInterceptor)
.build();
// Let's do the same call
request = new Request.Builder()
.url("http://httpbin.org/get")
.get()
.build();
response = client.newCall(request).execute();
response.close();
// Let's see if we had some issues with the cache
System.out.println("Is successful? " + response.isSuccessful());请注意,由于这是一个“独立”示例,我必须执行两次调用:
还要注意,您需要关闭响应,否则缓存将无法正确填充。
https://stackoverflow.com/questions/49002769
复制相似问题