HTTP 响应缓存在 Android 客户端中

HTTP response being cached in Android client

我有以下情况:

  1. 正在向我的远程服务器发送 http post(post 数据包含 json 字符串)请求。
  2. 在 json 中从我的服务器获取 http post 响应:{"result":true}
  3. 正在断开平板电脑中的所有互联网连接。
  4. 重复第 1 步中描述的 post 请求。
  5. 获得相同的缓存 "response" - {"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是一个成员变量。为什么需要一个成员变量来return这个结果。您可能 return 在第二次尝试时得到相同的结果。重新抛出您捕获的异常并让调用者处理它。