简体   繁体   English

改装缓存404响应

[英]Retrofit cache 404 response

I am using retrofit cache with okhttp3 as described in this answer : https://stackoverflow.com/a/23503804/6212796 , here's my intercepter 我正在使用okhttp3改型缓存,如此答案中所述: https ://stackoverflow.com/a/23503804/6212796,这是我的拦截器

private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
    @Override public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());
        if (Utils.isNetworkAvailable(context)) {
            int maxAge = 60; // read from cache for 1 minute
            return originalResponse.newBuilder()
                    .header("Cache-Control", "public, max-age=" + maxAge)
                    .build();
        } else {
            int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
            return originalResponse.newBuilder()
                    .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                    .build();
        }
    }

http client: http客户端:

OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);

    File httpCacheDirectory = new File(context.getCacheDir(), "responses");
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    Cache cache = new Cache(httpCacheDirectory, cacheSize);
    client.setCache(cache);

and the caching is working fine, but once I get a 404 response from the server retrofit get stuck with this response even if the server come to life again. 并且缓存工作正常,但是一旦我从服务器改造中获得404响应,即使服务器再次恢复正常,该响应也将卡住。

here's the log when the server is on : 这是服务器开启时的日志: 在此处输入图片说明

and this is the response when the server is down : 这是服务器关闭时的响应: 在此处输入图片说明

and this when it is up again : 并且当它再次出现时: 在此处输入图片说明

I had the same error in OkHttp 3.10.0. 我在OkHttp 3.10.0中有相同的错误。 I fixed it by removing the response from cache manually. 我通过手动从缓存中删除响应来修复它。 I created class in okhttp3 package. 我在okhttp3包中创建了类。 (It's important!) (这一点很重要!)

package okhttp3;

import android.content.Context;

import java.io.File;


class CacheUtil {
    static void remove(Context context, Request request) {
        try {
        Cache cache = new Cache(new File(context.getCacheDir(), "responses"), (10 * 1024 * 1024));//your cache folder
        cache.remove(request);
        } catch (Exception e) {
        }
    }
}

It can be added to your retrofit call enqueue, like this: 可以将其添加到您的改造调用队列中,如下所示:

    call.enqueue(new Callback<City>() {
        @Override
        public void onResponse(Call<City> call, Response<City> response) {
            if(response.isSuccessful()) {
                //execute success code
            } else {
                if (response.code() == 404) {
                    CacheUtil.remove(context, response.raw().request());
                }
            }
        }

        @Override
        public void onFailure(Call<City> call, Throwable t) {
            //execute failure code
        }
    });

Or it can be added to interceptor (which has to be added to OkHttpClient.Builder) like this: 或者可以将其添加到拦截器(必须将其添加到OkHttpClient.Builder中),如下所示:

new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        if (response.code() == 404) {
            CacheUtil.remove(context, request);
        }
        return response;
    }
}

May be this will help somebody. 也许这会帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM