如何与OKHttp使用缓存中的数据时,离线改造 [英] How Retrofit with OKHttp use cache data when offline
问题描述
我要改造与OkHttp使用缓存的时候没有互联网。
I want to Retrofit with OkHttp uses cache when is no Internet.
我prepare OkHttpClient是这样的:
I prepare OkHttpClient like this:
RestAdapter.Builder builder= new RestAdapter.Builder()
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Accept", "application/json;versions=1");
if (MyApplicationUtils.isNetworkAvaliable(context)) {
int maxAge = 60; // read from cache for 1 minute
request.addHeader("Cache-Control", "public, max-age=" + maxAge);
} else {
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
request.addHeader("Cache-Control",
"public, only-if-cached, max-stale=" + maxStale);
}
}
});
和这样的设置缓存:
Cache cache = null;
try {
cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
} catch (IOException e) {
Log.e("OKHttp", "Could not create http cache", e);
}
OkHttpClient okHttpClient = new OkHttpClient();
if (cache != null) {
okHttpClient.setCache(cache);
}
和我查了根深蒂固的设备,在高速缓存目录中保存文件的响应头和gzip文件。
and I checked on rooted device, that in cache directory are saving files with the "Response headers" and Gzip files.
但我不明白的离线改造缓存正确的答案,虽然在gzip文件是codeD我正确的答案。所以,我怎样才能使改造可以读取GZIP文件,他怎么能知道哪些文件应该是(因为我有几个文件有与其他反应)?
But I don't get the correct answer from retrofit cache in offline, although in GZip file is coded my correct answer. So how can I make Retrofit can read GZip file and how can he know which file it should be (because I have a few files there with other responses) ?
推荐答案
我在公司simlar问题:)
I have simlar problem in my company :)
问题是在服务器端。在serwer回应,我有:
The problem was on server side. In serwer response i have:
Pragma: no-cache
所以,当我删除了这一切开始工作。之前,我删除它,我得到的所有的时间这样的例外: 504不可满足要求(只-IF-缓存)
好了,所以如何实现在我身边的样子。
Ok so how implementation on my side looks.
OkHttpClient okHttpClient = new OkHttpClient();
File httpCacheDirectory = new File(appContext.getCacheDir(), "responses");
Cache cache = new Cache(httpCacheDirectory, maxSizeInBytes);
okHttpClient.setCache(cache);
OkClient okClient = new OkClient(okHttpClient);
RestAdapter.Builder builder = new RestAdapter.Builder();
builder.setEndpoint(endpoint);
builder.setClient(okClient);
如果您在测试问题,哪一方是问题(服务器或应用程序)。你可以用这样的feauture设置从服务器接收的头。
If you have problems in testing on which side is problem (server or app). You can use such feauture to set headers received from server.
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control",
String.format("max-age=%d", 60))
.build();
}
};
和简单地添加:
okHttpClient.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
由于是,你可以看到我能够删除杂注:无缓存
头测试时间
此外,我建议你阅读有关的Cache-Control
标题:
Also i suggest you to read about Cache-Control
header:
其他有用的链接:
这篇关于如何与OKHttp使用缓存中的数据时,离线改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!