How to prevent Android from returning a cached response to my HTTP Request?

人走茶凉 提交于 2019-11-26 15:59:10

问题


I'm writing a client that is making repeated http requests for xml data that is changing over time. It looks like the Android stack is caching my page requests and returning the same page repeatedly. How do I make sure it gets a fresh page each time?

-- code ---

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response;
    response = client.execute(request);

InputStream in;
in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

Thanks, Gerry


回答1:


add a HTTP header:

Cache-Control: no-cache

and see if that works.




回答2:


Append an unused parameter on the end of the URL:

HttpGet request = new HttpGet(url + "?unused=" + someRandomString());

where someRandomString() probably involves the current time.

It's crude, but it's pretty much guaranteed to work regardless of all the outside factors that can make a "proper" solution fail, like misconfigured or buggy proxies.




回答3:


Hint: to get the random string

HttpGet request = new HttpGet(url + "?unused=" + UUID.randomUUID().toString());


来源:https://stackoverflow.com/questions/2692814/how-to-prevent-android-from-returning-a-cached-response-to-my-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!