How to tell OkHttpClient to ignore cache and force refresh from server?

inder picture inder · Mar 14, 2015 · Viewed 12.7k times · Source

In my android application, I am using Retrofit with OkHttpClient with caching enabled to access some APIs. Some of our APIs sometimes return empty data. We provide a "Refresh" button in the app for the client to reload data from a specific API.

How do I tell OkHttpClient that a specific request should ignore the cached entry. Alternatively, is there a mechanism to delete the cached response corresponding to a single request?

I see Cache.remove(request) method but it is marked as private.

Answer

inder picture inder · Mar 14, 2015

As Jake Wharton suggested in issues, do this to ignore the cache:

request.setCacheControl(CacheControl.FORCE_NETWORK);