Lollipop中不起作用

Lollipop中不起作用

本文介绍了HttpResponseCache在Android Lollipop中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序HttpResponseCache中成功使用了,但是当我的手机更新为Lollipop时,我意识到HttpResponseCache现在永远不会被命中",总是执行网络请求.我已经确认,在Lollipop之前的Android版本中,Lollipop仍然可以正常工作.也许这是我做错了,随着新的Android更改它已经出现了.

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.

有人有什么主意吗?

我的代码:

应用程序类,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:" +
        }
    }

管理请求的功能

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;
}

每次请求后

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

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

示例请求标头:

  • 缓存控制→max-age = 300
  • 连接→保持活动状态
  • 内容编码→gzip
  • 内容类型→application/json; charset = utf-8
  • 日期→星期三,2015年4月8日格林尼治标准时间
  • Expires→周三,2015年4月8日格林尼治标准时间
  • 最后修改→2015年4月8日,星期三,格林尼治标准时间
  • 服务器→nginx
  • 传输编码→分块
  • 变化→接受编码
  • X缓存→MISS
  • 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

推荐答案

在经历了几天这个问题之后,我遇到了这个问题.再次在棉花糖中固定.

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

这是棒棒糖中的一个错误,其中Vary-> Accept-Encoding标头破坏了缓存,因为默认情况下会填充Accept-Encoding而不将其写掉.

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.

这里是问题的链接:

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

解决方法是显式设置Accept-Encoding:

The fix is to set the Accept-Encoding explicitly:

接受编码-> gzip

Accept-Encoding -> gzip

接受编码->身份

在阅读方面,您必须将其添加到输入流的阅读中:

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();

这篇关于HttpResponseCache在Android Lollipop中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:46