我一直在我的应用程序中成功使用 HttpResponseCache,但是当我的手机更新到 Lollipop 时,我意识到 HttpResponseCache 现在永远不会命中",总是执行网络请求.我已经确认在 Lollipop 之前的 Android 版本中仍然运行良好.也许是我做错了什么,随着新的 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.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
- 内容类型→应用程序/json;字符集=utf-8
- 日期 → 2015 年 4 月 8 日,星期三,格林威治标准时间 12:37:35
- 到期 → 格林威治标准时间 2015 年 4 月 8 日星期三 12:42:35
- 上次修改时间 → 2015 年 4 月 8 日,星期三 12:37:35 GMT
- 服务器 → nginx
- 传输编码 → 分块
- 变化 → 接受编码
- X 缓存 → 未命中
在遇到这个问题几天后,我遇到了这个问题.它在 Marshmallow 中再次修复.
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.
这是问题的链接:
code.google/p/android/问题/详细信息?id=162475
修复方法是显式设置接受编码:
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 中不起作用
发布评论