In Android okhttpclient not caching public cache-control

陌路散爱 提交于 2020-01-17 12:25:48

问题


  1. I am using below code to cache in Android device to collect the response and get from cache till the max age expires. When i use "public" I was able to see the response cached in my application installed folder(/data/data/app_folder).

  2. I tried all below none of them works for "private" but when I change to "public" all my solutions i tried works.

  3. But I need to make it to work for "private". Am I missing something.

  4. Header I will receive "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:

  1. And I am using Retrofit library, an Android client.

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

来源:https://stackoverflow.com/questions/29435766/in-android-okhttpclient-not-caching-public-cache-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!