Android 使用改进2和OkHttp 3的脱机响应缓存
我试图通过OkHttp和改型缓存响应。我知道有几个问题与我的问题类似,但没有一个能够解决我的问题 下面是我的拦截器,负责修改标题Android 使用改进2和OkHttp 3的脱机响应缓存,android,caching,retrofit2,okhttp3,Android,Caching,Retrofit2,Okhttp3,我试图通过OkHttp和改型缓存响应。我知道有几个问题与我的问题类似,但没有一个能够解决我的问题 下面是我的拦截器,负责修改标题 private static class CachingControlInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request
private static class CachingControlInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response originalResponse = chain.proceed(request);
return originalResponse.newBuilder()
.header("Cache-Control", (UtilityMethods.isNetworkAvailable()) ?
"public, max-age=60" : "public, max-stale=604800")
.build();
}
}
现在,这在第一种情况下非常有效-
UnknownHosteException
)“公共,最大年龄=60”
)完成
但是,不知何故,“public,max age=60”
部分根本不起作用
此部分应使okhttp能够在设备脱机时获取一周前的过时数据,但相反,我得到了
UnknownHostException
我想这就是您想要的:
.header("Cache-Control", (UtilityMethods.isNetworkAvailable()) ?
"public, max-age=60" : "public, only-if-cached, max-stale=604800")
当网络不可用时,仅当缓存指令时才添加
。这只接受缓存中的响应。我想这就是您要寻找的:
.header("Cache-Control", (UtilityMethods.isNetworkAvailable()) ?
"public, max-age=60" : "public, only-if-cached, max-stale=604800")
当网络不可用时,仅当缓存
指令时才添加。这只接受缓存中的响应