我一直在我的应用程序中成功使用 HttpResponseCache,但是当我的手机更新到 Lollipop 时,我意识到 HttpResponseCache 现在永远不会“命中”,总是执行网络请求。我已经确认在 Lollipop 之前的 Android 版本中仍然运行良好。 可能是我做错了什么,随着新的 Android 更改出现了。
有人知道吗?
我的代码:
应用类,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();
}
}
示例请求 header :
- 缓存控制 → max-age=300
- 连接 → 保持活跃
- 内容编码 → gzip
- 内容类型 → application/json;字符集=utf-8
- 日期 → 2015 年 4 月 8 日星期三 12:37:35 GMT
- 到期 → 格林威治标准时间 2015 年 4 月 8 日星期三 12:42:35
- 上次修改时间 → 2015 年 4 月 8 日星期三 12:37:35 GMT
- 服务器 → nginx
- 传输编码 → 分块
- 变化 → 接受编码
- X-Cached → MISS
最佳答案
遇到这个问题几天后,我遇到了这个问题。它在 Marshmallow 中再次修复。
这是 Lollipop 中的一个错误,其中 Vary -> Accept-Encoding header 会破坏缓存,因为默认情况下会填充 Accept-Encoding 但不会被写入。
这是问题的链接:
https://code.google.com/p/android/issues/detail?id=162475
解决方法是显式设置 Accept-Encoding:
接受编码 -> gzip
或
接受编码 -> 身份
在读取端,您必须将此添加到输入流的读取中:
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();
关于android - HttpResponseCache 在 Android Lollipop 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29517298/