9

I use retrofit and okhttp in one of our applications.

I can't really find a good explanation for the default behaviour of Retrofit.

If Okhttp is on the class path it will be automatically used. But as far as I can see it the default HttpResponseCache is null.

Do I need to explicitly enable caching with Retrofit and Okhttp?

Janusz
  • 187,060
  • 113
  • 301
  • 369

3 Answers3

15

Correct implementation for OkHttpClient v2:

int cacheSize = 10 * 1024 * 1024; // 10 MiB
File cacheDir = new File(context.getCacheDir(), "HttpCache");
Cache cache = new Cache(cacheDir, cacheSize);
OkHttpClient client = new OkHttpClient.Builder()
    .cache(cache)
    .build();

see documentation

e.shishkin
  • 1,173
  • 12
  • 9
8

DEPRECATED for OkHttpClient v2.0.0 and higher

As Jesse Wilson pointed out you need to create your own cache.
The following code should create a 10MB cache.

File httpCacheDirectory = new File(application.getApplicationContext()
    .getCacheDir().getAbsolutePath(), "HttpCache");

HttpResponseCache httpResponseCache = null;
try {
   httpResponseCache = new HttpResponseCache(httpCacheDirectory, 10 * 1024);
} catch (IOException e) {
   Log.e(getClass().getSimpleName(), "Could not create http cache", e);
}

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setResponseCache(httpResponseCache);
builder.setClient(new OkClient(okHttpClient));

The code is based on Jesse Wilsons example on Github.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • if this is set on a post would it be ignored? because I am setting it on my general builder which is used for every request – Lion789 Mar 17 '14 at 00:24
  • http://stackoverflow.com/questions/22445177/trying-to-make-use-of-httpcache-android I am actually getting this error... and if I do not use httpResponseCache.install it errors that new HttpResponseCache cannot be used outside... it is not public.. – Lion789 Mar 17 '14 at 00:46
  • 2
    isn't that a 10MB cache? – Nelson Osacky Jun 26 '14 at 18:48
  • Yes looks more like a 10MB Cache ;) – Janusz Jul 17 '14 at 14:56
  • I saw the docs and found that `HttpResponseCache` was added in API Level 13, so does `okHttp` caches below API Level 13? If yes, how? – Ram Patra Jan 05 '15 at 10:16
  • 1
    @NelsonOsacky @Janusz Actually no, it's 10KB, not 10MB. Take a look here: http://developer.android.com/reference/android/net/http/HttpResponseCache.html `long httpCacheSize = 10 * 1024 * 1024; // 10 MiB` The same rule applies to OkHttp's `Cache` constructor. – EyesClear May 18 '15 at 16:54
  • 1
    fyi this is a deprecated answer http://square.github.io/okhttp/1.x/okhttp/com/squareup/okhttp/OkHttpClient.html#setResponseCache(java.net.ResponseCache) – Blundell May 09 '16 at 21:06
7

You should manually create your OkHttpClient and configure it how you like. In this case you should install a cache. Once you have that create an OkClient and pass it to Retrofit's RestAdapter.Builder

Also, no caching for HTTP POST requests. GETs will be cached, however.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • 1
    I added the code I used in separate answer because I think this would be interesting for other people as well. Since I copied it from your example I hope the code is ok. – Janusz Feb 06 '14 at 07:42
  • 1
    http://stackoverflow.com/questions/22445177/trying-to-make-use-of-httpcache-android says that there is no need to configure the cache anymore. Is this correct? – Janusz Nov 20 '14 at 16:38