2013-04-10 109 views
0

在我的Android应用程序中,我试图缓存Http客户端的响应 当我调用url时,控制台始终显示“响应来自上游服务器”消息。 我对HTTP客户端代码如下:如何在Android应用中缓存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()); 
    } 

回答

-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”。

+0

根据developer.android.com上的文档HttpResponseCache类支持HttpURLConnection和HttpsURLConnection; DefaultHttpClient或AndroidHttpClient没有平台提供的缓存。 – 2013-04-11 05:07:39

+0

所以,如果我没有错,你写的代码块只适用于httpUrlConnection而不是httpClient,我正在使用它。 – 2013-04-11 05:08:59

相关问题