Android avoid caching

2019-07-16 12:46发布

// 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);

When I make a server call, it brings data...second time i call it, it brings it cached.. and doesn't call server... how can i fix this?

i don't want caching.

2条回答
smile是对你的礼貌
2楼-- · 2019-07-16 12:49

You can add a HTTP header and have :

Cache-Control: no-cache

like

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

visit http://developer.android.com/reference/android/net/http/HttpResponseCache.html

查看更多
疯言疯语
3楼-- · 2019-07-16 13:12

You can add following HTTP header to your request: Cache-Control: no-cache

httpGet.addHeader("Cache-Control", "no-cache");
查看更多
登录 后发表回答