【问题标题】:Android avoid cachingAndroid避免缓存
【发布时间】:2014-03-25 16:44:56
【问题描述】:
// http client
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);

当我进行服务器调用时,它会带来数据......我第二次调用它时,它会将它缓存......并且不调用服务器...... 我该如何解决这个问题?

我不想缓存。

【问题讨论】:

    标签: android http caching networking


    【解决方案1】:

    您可以在请求中添加以下 HTTP 标头:Cache-Control: no-cache

    httpGet.addHeader("Cache-Control", "no-cache");
    

    【讨论】:

      【解决方案2】:

      您可以添加一个 HTTP 标头并拥有:

      Cache-Control: no-cache
      

      喜欢

      httpPost.addHeader("Cache-Control", "no-cache");
      httpGet.addHeader("Cache-Control", "no-cache");
      

      访问 http://developer.android.com/reference/android/net/http/HttpResponseCache.html

      【讨论】:

      • @user3278732 如果您发现此答案对 SO 标准很有帮助,您应该通过投票或接受来感谢它。
      猜你喜欢
      • 2018-10-20
      • 1970-01-01
      • 2012-05-09
      • 2016-12-19
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      相关资源
      最近更新 更多