Android 如何使用已安装的HttpResponseCache
我遵循了,并通过在应用程序的onCreate()方法中包含以下代码安装了HttpResponseCache:Android 如何使用已安装的HttpResponseCache,android,caching,httpurlconnection,httpresponsecache,Android,Caching,Httpurlconnection,Httpresponsecache,我遵循了,并通过在应用程序的onCreate()方法中包含以下代码安装了HttpResponseCache: try { File httpCacheDir = new File(context.getCacheDir(), "http"); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOExce
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)
。有必要吗?只要您在连接上调用setUseCaches(true)
,安装的HttpResponseCache将由HttpURLConnection
和HttpsURLConnection
使用
此外,web服务器的响应需要可缓存。我已使用HTTPClient进行请求。如何在此处使用HttpResponseCatch?不需要setUseCaches(true)