0

在我的 android 应用程序中,我试图缓存 Http 客户端 的响应当我调用 url 时,控制台总是显示“响应来自上游服务器”消息。我的 Http Client 代码如下:

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());
    }
4

1 回答 1

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

于 2013-04-10T12:53:10.470 回答