2013-04-10 114 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

相關問題