簡體   English   中英

HTTP響應被緩存在Android客戶端中

[英]HTTP response being cached in Android client

我有以下情況:

  1. 向我的遠程服務器發送http發布(發布數據包含json字符串)請求。
  2. 在json中從我的服務器獲取http發布響應:{“ result”:true}
  3. 斷開平板電腦中的所有互聯網連接。
  4. 重復步驟1中所述的發布請求。
  5. 獲取相同的緩存“響應”-{“ result”:true},這是我所不希望得到的...我不希望我的http客戶端緩存任何數據。 我期望得到null或類似的東西。

如何防止HTTP客戶端緩存數據?

我的服務處理程序如下所示:

public String makeServiceCall(String url, int method,
        List<NameValuePair> params, String requestAction) {
    try {      

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);

            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }
            httpResponse = httpClient.execute(httpPost);
        }

        else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);
        }

        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
       // Toast.makeText(Globals.getContext(), "check your connection", Toast.LENGTH_SHORT).show();
    }        
    return response;

}

我只是注意到response是一個成員變量。 為什么需要一個成員變量來返回此結果。 您可能在第二次嘗試中返回了相同的結果。 重新引發捕獲的異常,然后由調用方處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM