【发布时间】:2019-03-25 18:24:27
【问题描述】:
我阅读了数十篇教程和 Stackoverflow 对我的问题的回答,但对我没有任何帮助!此外,它们中的大多数都是旧的,所以 OKHTTP 可能以某种方式发生了变化。
我只想为 Retrofit 启用 离线缓存。
我正在使用 GET
我尝试只使用 offlineCacheInterceptor 作为拦截器,但我不断收到:
Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname
我尝试使用offlineCacheInterceptor作为拦截器+provideCacheInterceptor()作为网络拦截器的组合,但我不断得到:
504 Unsatisfiable Request (only-if-cached) and a null response.body()
我什至确保在任何地方都添加.removeHeader("Pragma")!
我尝试了所有这些链接:
https://newfivefour.com/android-retrofit2-okhttp3-cache-network-request-offline.html(一个拦截器,不工作!!)
https://medium.com/mindorks/caching-with-retrofit-store-responses-offline-71439ed32fda(一个拦截器,不工作!)
https://caster.io/lessons/retrofit-2-offline-cache(单独在线+离线缓存,不工作)
https://www.journaldev.com/23297/android-retrofit-okhttp-offline-caching(不工作,504 Unsatisfiable Request (only-if-cached))
http://mikescamell.com/gotcha-when-offline-caching-with-okhttp3/(一个拦截器,不工作!!)
https://stackoverflow.com/a/48295397/8086424(不工作) 无法解析主机“jsonplaceholder.typicode.com”:没有与主机名关联的地址
Can Retrofit with OKHttp use cache data when offline(太混乱了!)
这是我的代码:
public static Retrofit getRetrofitInstance(Context context) {
if (retrofit == null) {
c = context;
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(context.getCacheDir(), cacheSize);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(provideHttpLoggingInterceptor())
.addInterceptor(offlineCacheInterceptor)
.addNetworkInterceptor(provideCacheInterceptor())
.cache(cache)
.build();
//////////////////////////
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit;
}
public static Interceptor offlineCacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e("bbbb", "bbbb");
if (!checkInternetAvailability()) {
Log.e("aaaaa", "aaaaaa");
CacheControl cacheControl = new CacheControl.Builder()
.maxStale(30, TimeUnit.DAYS)
.build();
request = request.newBuilder()
.cacheControl(cacheControl)
.removeHeader("Pragma")
.build();
}
return chain.proceed(request);
}
};
public static Interceptor provideCacheInterceptor() {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
// re-write response header to force use of cache
CacheControl cacheControl = new CacheControl.Builder()
.maxAge(2, TimeUnit.MINUTES)
.build();
return response.newBuilder()
.header(CACHE_CONTROL, cacheControl.toString())
.removeHeader("Pragma")
.build();
}
};
}
我正在使用返回的 jsonplaceholder.typicode.com/photos:
content-type: application/json; charset=utf-8
date: Sun, 21 Oct 2018 14:26:41 GMT
set-cookie: __cfduid=d9e935012d2f789245b1e2599a41e47511540132001; expires=Mon, 21-Oct-19 14:26:41 GMT; path=/; domain=.typicode.com; HttpOnly
x-powered-by: Express
vary: Origin, Accept-Encoding
access-control-allow-credentials: true
expires: Sun, 21 Oct 2018 18:26:41 GMT
x-content-type-options: nosniff
etag: W/"105970-HCYFejK2YCxztz8++2rHnutkPOQ"
via: 1.1 vegur
cf-cache-status: REVALIDATED
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 46d466910cab3d77-MXP
Cache-Control: public, max-age=60
【问题讨论】:
-
嗨,伙计!看来这是您需要的解决方案 stackoverflow.com/a/48208801/4374863 它解决了“没有与主机名关联的地址” - 前段时间对我来说是错误
标签: android caching retrofit retrofit2 okhttp3