在我的Android应用程序中,我试图缓存Http客户端的响应 当我调用url时,控制台始终显示“响应来自上游服务器”消息。 我的Http客户端代码如下:
try
{
CacheConfig cacheConfig = new CacheConfig();
cacheConfig.setMaxCacheEntries(1000);
cacheConfig.setMaxObjectSizeBytes(8192);
DefaultHttpClient realClient = new DefaultHttpClient();
realClient.addResponseInterceptor(MakeCacheable.INSTANCE, 0); // This goes first
CachingHttpClient httpClient = new CachingHttpClient(realClient, cacheConfig);
HttpContext localContext = new BasicHttpContext();
String requestUrl= SERVER_IP+ ANDROID_REQUEST_URL+METHOD_GET_ALLDEALS;
HttpPost httppost = new HttpPost(requestUrl);
ArrayList<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
BasicNameValuePair latitudeValue=new BasicNameValuePair("lat",String.valueOf(28.460190279993547));
BasicNameValuePair longitudeValue=new BasicNameValuePair("lng",String.valueOf(77.0645221799944));
BasicNameValuePair radiusValue=new BasicNameValuePair("radius",String.valueOf(10));
BasicNameValuePair catIdValue=new BasicNameValuePair("cat",String.valueOf(1));
BasicNameValuePair subCategoryIdValue=new BasicNameValuePair("subcat",String.valueOf(0));
BasicNameValuePair offsetValue=new BasicNameValuePair("offset",String.valueOf(0));
BasicNameValuePair deviceIdvalue=new BasicNameValuePair("deviceId","12366A4C-9CD0-47F4-9ACC-D1CD7DD997FC");
BasicNameValuePair sessionIdValue=new BasicNameValuePair("sessionId","10873");
nameValuePairs.add(latitudeValue);
nameValuePairs.add(longitudeValue);
nameValuePairs.add(radiusValue);
nameValuePairs.add(catIdValue);
nameValuePairs.add(subCategoryIdValue);
nameValuePairs.add(offsetValue);
nameValuePairs.add(deviceIdvalue);
nameValuePairs.add(sessionIdValue);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String paramString = URLEncodedUtils.format(nameValuePairs,"utf-8");
String url = requestUrl+"?"+paramString;
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(httpget,localContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
result = readIncomingData(instream);
instream.close();
Log.i("MainActivity", result);
}
CacheResponseStatus responseStatus = (CacheResponseStatus) localContext.getAttribute(
CachingHttpClient.CACHE_RESPONSE_STATUS);
switch (responseStatus) {
case CACHE_HIT:
Log.e("","A response was generated from the cache with no requests " +
"sent upstream");
break;
case CACHE_MODULE_RESPONSE:
Log.e("","The response was generated directly by the caching module");
break;
case CACHE_MISS:
Log.e("","The response came from an upstream server");
break;
case VALIDATED:
Log.e("","The response was generated from the cache after validating " +
"the entry with the origin server");
break;
}
}catch (Exception e) {
Log.i("MainActivity",e.getMessage());
}
答案 0 :(得分:-1)
我使用以下代码块并在onCreate()
中调用它private void enableHttpResponseCache() {
try {
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
File httpCacheDir = new File(getCacheDir(), "http");
Class.forName("android.net.http.HttpResponseCache")
.getMethod("install", File.class, long.class)
.invoke(null, httpCacheDir, httpCacheSize);
Log.i(LOGTAG,"cache");
} catch (Exception httpResponseCacheNotAvailable) {
Log.i(LOGTAG,"nocache");
}
}
请记住,这只会在4.0+设备上启用缓存。低于此值,您将在日志中看到“nocache”。