我遵循开发人员指南并安装了一个HttpResponseCache,在我的应用程序的onCreate()方法中包含了以下代码:
try {
File httpCacheDir = new File(context.getCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
} catch (IOException e) {
Log.i(TAG, "HTTP response cache installation failed:" + e);
}现在,在某个时候,我在这个应用程序中启动一个活动并使用以下代码获取一个URL:
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}当我获取URL来告诉它使用已安装的缓存时,我必须做些什么吗?或者,在此应用程序中启动的任何活动都会自动使用吗?
例如,在此示例代码中,他们称之为connection.setUseCaches(true)。这有必要吗?
发布于 2014-06-29 10:50:42
只要您在连接上调用HttpResponseCache,HttpURLConnection和HttpsURLConnection就会使用已安装的setUseCaches(true)。
另外,来自web服务器的响应需要是可缓存的。
https://stackoverflow.com/questions/24418976
复制相似问题