How to prevent Android from returning a cached res

2019-01-11 01:04发布

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

3条回答
Viruses.
2楼-- · 2019-01-11 01:25

Hint: to get the random string

HttpGet request = new HttpGet(url + "?unused=" + UUID.randomUUID().toString());
查看更多
在下西门庆
3楼-- · 2019-01-11 01:32

add a HTTP header:

Cache-Control: no-cache

and see if that works.

查看更多
We Are One
4楼-- · 2019-01-11 01:33

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.

查看更多
登录 后发表回答