34

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.

inder
  • 1,774
  • 1
  • 15
  • 15

2 Answers2

30

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

request.setCacheControl(CacheControl.FORCE_NETWORK);
gokhanakkurt
  • 4,935
  • 4
  • 28
  • 39
inder
  • 1,774
  • 1
  • 15
  • 15
7
@Headers("Cache-Control: no-cache") 

on the method should work. If you want to do it dynamically you can add a

@Header("Cache-Control") 

String cacheControl parameter and pass null or "no-cache"

EzLo
  • 13,780
  • 10
  • 33
  • 38
Rasheed
  • 542
  • 1
  • 6
  • 14