在Android okhttpclient中没有缓存公共缓存控制

时间:2015-04-03 16:12:22

标签: android header private cache-control

  1. 我使用下面的代码在Android设备中缓存以收集响应并从缓存中获取直到最大年龄到期。当我使用“public”时,我能够在我的应用程序安装文件夹(/ data / data / app_folder)中看到缓存的响应。

  2. 我尝试了以下所有内容,但没有一个适用于“私人”,但当我改为“公开”时,我尝试了所有解决方案。

  3. 但我需要让它为“私人”工作。我错过了什么。

  4. 标题我将收到“Cache-Control”,“private,max-age = 120”

  5. 解决方案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);
    

    解决方案2:

    1. 我正在使用Retrofit库,一个Android客户端。

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

0 个答案:

没有答案