1

I am using retrofit cache with okhttp3 as described in this answer : https://stackoverflow.com/a/23503804/6212796, here's my intercepter

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:

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.

here's the log when the server is on : enter image description here

and this is the response when the server is down : enter image description here

and this when it is up again : enter image description here

1 Answers1

1

I had the same error in OkHttp 3.10.0. I fixed it by removing the response from cache manually. I created class in okhttp3 package. (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:

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.

Eugene Babich
  • 1,271
  • 15
  • 25