嗨,我正在尝试使用在Android4中引入的HttpResponseCache。文档中确实清楚地谈到了如何安装缓存,但我完全不知道如何缓存从net.Earlier下载的图像,我正在使用DiskLruCache来缓存它们。谁能给我举一些使用HttpResponseCache的工作代码的例子。
编辑:-谁能告诉我我在这里做错了什么:
MainActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
final File httpCacheDir = new File(getCacheDir(), "http");
try {
Class.forName("android.net.http.HttpResponseCache")
.getMethod("install", File.class, long.class)
.invoke(null, httpCacheDir, httpCacheSize);
Log.v(TAG,"cache set up");
} catch (Exception httpResponseCacheNotAvailable) {
Log.v(TAG, "android.net.http.HttpResponseCache not available, probably because we're running on a pre-ICS version of Android. Using com.integralblue.httpresponsecache.HttpHttpResponseCache.");
try{
com.integralblue.httpresponsecache.HttpResponseCache.install(httpCacheDir, httpCacheSize);
}catch(Exception e){
Log.v(TAG, "Failed to set up com.integralblue.httpresponsecache.HttpResponseCache");
}
}
TheMainListFrag gf=(TheMainListFrag) getSupportFragmentManager().findFragmentByTag("thelistfrags");
if(gf==null){
gf=TheMainListFrag.newInstance();
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.thelefty, gf,"thelistfrags");
ft.commit();
}
}然后在TheMainListFrag的加载器中,我执行以下操作:
public ArrayList<HashMap<String,String>> loadInBackground() {
String datafromServer = null;
ArrayList<HashMap<String,String>> al = new ArrayList<HashMap<String,String>>();
try {
String url = "someurl";
HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
InputStream is = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
datafromServer=sb.toString();
Log.v("fromthread",datafromServer);
// etc
//etc
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
Log.v("fromthread", e.getClass() + "--" + e.getMessage());
}
return al;
}当我连接到互联网时,它工作得很好,并且在http目录-上面提到的缓存目录中,我也可以看到这些文件。但是当我没有连接到互联网时,数据就会拒绝加载。
当我从网络上加载图像时,我看到名为.tmp的缓存文件,我认为它按照DiskLruCache被称为脏文件。
如果您还需要我提供任何其他信息,请让我知道
发布于 2012-07-30 08:12:26
在HttpResponseCache documentation上强制缓存响应一节中
有时,如果资源立即可用,您可能会想要显示它们,但不是其他情况。可以使用它,这样您的应用程序就可以在等待下载最新数据时显示一些内容。要将请求限制为本地缓存的资源,请添加
only-if-cached指令:
try {
connection.addRequestProperty("Cache-Control", "only-if-cached");
InputStream cached = connection.getInputStream();
// the resource was cached! show it
} catch (FileNotFoundException e) {
// the resource was not cached
}这项技术在有过时的响应比没有响应的情况下效果更好。要允许过时的缓存响应,请使用以秒为单位的最大过期时间的
max-stale指令:
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
connection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);发布于 2012-07-21 02:21:30
当您启用HttpResponseCache时,所有HttpUrlConnection查询都将被缓存。您不能使用它来缓存任意数据,因此我建议继续使用DiskLruCache。
发布于 2013-11-01 00:52:35
在我的例子中,HttpResponseCache实际上并没有缓存任何东西。修复它的方法很简单:
connection.setUseCaches(true);(在建立连接之前,必须在HttpURLConnection上调用此函数。)
对于更细粒度的控制,可以使用max-stale作为Jesse Wilson pointed out。
https://stackoverflow.com/questions/11584761
复制相似问题