我已经成功地在我的应用程序HttpResponseCache中使用了,但是当我的手机升级到Lollipop时,我意识到HttpResponseCache现在永远不会被“击中”,总是执行网络请求。我已经证实,在Android版本中,pre Lollipop仍然运行良好。也许是我做错了什么,随着新的安卓系统的改变,它已经出现了。
有人知道吗?
我的代码:
应用程序类,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();
}
}示例请求头:
发布于 2015-11-04 09:22:26
在遇到这个问题几天后,我遇到了这个问题。它又在马斯马洛修好了。
这是棒棒糖中的一个bug,在这种情况下,可变->接受-编码头会破坏缓存,因为默认情况下接受-编码会被填充,但不会被写入。
下面是这个问题的链接:
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();发布于 2017-12-12 11:06:44
我也有过类似的问题。我原以为图像会被缓存,但事实并非如此。
事实证明,问题在于,在将 InputStream读入位图后,我并没有关闭它。
您的fetchInputStream返回它从http连接获得的InputStream,请确保正确关闭它。
在关闭连接的InputStream之前,android缓存不会保存资源。
https://stackoverflow.com/questions/29517298
复制相似问题