OkHttpClient cannot enable setCache method

I am trying to set the cache for Retrofit so that it does not need to constantly retrieve data. I followed this SO as it seems to be in the right direction of what I need.

I have the following (which is identical to SO)

OkHttpClient client = new OkHttpClient(); client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR); File httpCacheDirectory = new File(getCacheDir(), "responses"); int cacheSize = 10*1024*1024; Cache cache = new Cache(httpCacheDirectory, cacheSize); client.setCache(cache); 

However, client.setCache(cache) returns a cannot resolve method setCache .

What am I doing wrong here? I have modification 2.1.0 and okhttp3 3.4.1

+6
source share
1 answer

In 3.x, the set of methods on OkHttpClient was moved to methods on OkHttpClient.Builder . You want something like this:

 File httpCacheDirectory = new File(getCacheDir(), "responses"); int cacheSize = 10*1024*1024; Cache cache = new Cache(httpCacheDirectory, cacheSize); OkHttpClient client = new OkHttpClient.Builder() .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .cache(cache) .build(); 
+12
source

All Articles