1
votes

I am caching the HTTP response Using Retrofit2 and OKHttp. Here is my code:

    int cacheSize = 10 * 1024 * 1024;
    Cache cache = new Cache(application.getCacheDir(), cacheSize);

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().
cache(cache).addNetworkInterceptor(interceptor).build();

Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();

I am getting the response headers Cache-control and Expires from our backend REST API's.

Now i want to get the server Response by bypassing the the Expires Header. Please help me with this problem.

1

1 Answers

1
votes

Add this header to your HTTP request:

Cache-Control: no-cache

You can do this with Retrofit’s @Header or with an OkHttp interceptor.