Android okhttpclient does not cache general cache control

  • I use the below code for caching on an Android device to collect the response and receive from the cache until the maximum age has passed. When I use "public", I was able to see the answer in the cache in the installed application folder (/ data / data / app_folder).

  • I tried everything below, none of them work for "private", but when I go to the "public" all my solutions, I tried to work.

  • But I need to make it work for "private". Did I miss something.

  • The title I get is "Cache-Control", "private, max-age = 120"

Solution 1:

File httpCacheDirectory = new File(context.getCacheDir(), "responses"); HttpResponseCache httpResponseCache = null; try { httpResponseCache = new HttpResponseCache(httpCacheDirectory, 10 * 1024 * 1024); } catch (IOException e) { Log.e("Retrofit", "Could not create http cache", e); } OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setResponseCache(httpResponseCache); 

Solution 2:

  • And I use the Retrofit library, an Android client.

     Cache cache = new Cache(cacheDirectory, cacheSize); client = new OkHttpClient(); client.setCache(cache); 
0
source share

All Articles