0

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());
    }
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 に答える