HttpResponseCache not working in Android Lollipop

不羁的心 提交于 2019-11-30 15:50:13

问题


I've been using in my app HttpResponseCache successfully, but when my phone updated to Lollipop I realized that HttpResponseCache now never get "hit", always do the network request. I've confirmed that in Android versions pre Lollipop are still working well. Maybe it's something that I did wrong and with new Android changes it has been appeared.

Has anyone any idea?

My code:

Application class, onCreate...

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        try {
            File httpCacheDir = new File(getApplicationContext().getCacheDir()
                    , "http");
            long httpCacheSize = 10 * 1024 * 1024;
            HttpResponseCache.install(httpCacheDir, httpCacheSize);
        } catch (IOException e) {
            Log.i(TAG, "HTTP response cache installation failed:" + e);
        }
    } else {
        try {
            File httpCacheDir = new File(getCacheDir(), "http");
            long httpCacheSize = 10 * 1024 * 1024;
            Class.forName("android.net.http.HttpResponseCache")
                    .getMethod("install", File.class, long.class)
                    .invoke(null, httpCacheDir, httpCacheSize);
        } catch (Exception e) {
                Log.i(TAG, "HTTP response cache installation failed:" + 
        }
    }

Function to manage request

public static InputStream fetchInputStream(String strURL, boolean forceRefresh)
        throws IOException {

    HttpURLConnection mHttpConn = null;
    InputStream inputStream = null;
    URL url = new URL(strURL);
    HttpResponseCache cache;

    try {
        mHttpConn = (HttpURLConnection) url.openConnection();

        if (forceRefresh) {
            mHttpConn.addRequestProperty("Cache-Control", "no-cache");
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            cache = HttpResponseCache.getInstalled();
            if (cache != null) {

                    Log.i("TEST CACHE", "TEST PETICION: Req count: "
                            + cache.getRequestCount() + ", hit count "
                            + cache.getHitCount() + ", netWork count "
                            + cache.getNetworkCount() + "   size = "
                            + cache.size() + " <-----------------");

            }
        }

        mHttpConn.setUseCaches(true);
        mHttpConn.setDefaultUseCaches(true);
        mHttpConn.setRequestMethod("GET");
        mHttpConn.setConnectTimeout(30000);
        mHttpConn.setReadTimeout(30000);
        mHttpConn.connect();

        if (mHttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = mHttpConn.getInputStream();
        }


    } catch (IOException ex) {
        Log.e("NetworkConnectionManager InputStream", "Exception opening ["
                + strURL + "] ->", ex);
        mHttpConn.disconnect();

        throw ex;
    }

    return inputStream;
}

After every request

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        HttpResponseCache cache = HttpResponseCache.getInstalled();

        if (cache != null) {
            cache.flush();
        }
    }

Example request header:

  • Cache-Control → max-age=300
  • Connection → keep-alive
  • Content-Encoding → gzip
  • Content-Type → application/json; charset=utf-8
  • Date → Wed, 08 Apr 2015 12:37:35 GMT
  • Expires → Wed, 08 Apr 2015 12:42:35 GMT
  • Last-Modified → Wed, 08 Apr 2015 12:37:35 GMT
  • Server → nginx
  • Transfer-Encoding → chunked
  • Vary → Accept-Encoding
  • X-Cached → MISS

回答1:


After having this problem for days I ran into this issue. It is fixed again in Marshmallow.

It is a bug in lollipop where the Vary - > Accept-Encoding header breaks the cache because Accept-Encoding gets filled in by default but not written away.

Here is a link to the issue:

https://code.google.com/p/android/issues/detail?id=162475

The fix is to set the Accept-Encoding explicitly:

Accept-Encoding -> gzip

or

Accept-Encoding -> identity

On the reading side you have to add this to the reading of the input stream:

String encoding = urlConnection.getHeaderField("Content-Encoding");
boolean gzipped = encoding!=null && encoding.toLowerCase().contains("gzip");
Inputstream inputStream;
if(gzipped)
    inputStream = new GZIPInputStream(urlConnection.getInputStream());
else
    inputstream = urlConnection.getInputStream();



回答2:


I've had similar problem. I was expecting images to be cached but they weren't.

What turned out to be the problem was that I was not closing InputStream after it being read into a Bitmap.

Your fetchInputStream returns an InputStream that it got from http connection, make sure you close it properly.

The android http cache will not save a resource until you close the connection's InputStream.



来源:https://stackoverflow.com/questions/29517298/httpresponsecache-not-working-in-android-lollipop

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