views:

92

answers:

2

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  A: 

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.

RichieHindle
+1  A: 

add a HTTP header:

Cache-Control: no-cache

and see if that works.

Kylar
Using no-cache and no-store does not seem to effect the results.Adding a bogus parameter that changes does not seem to work either.I'm using:HttpClient client = new DefaultHttpClient();HttpGet request = new HttpGet(url);HttpResponse response;try { request.setHeader("Cache-Control", "no-cache"); request.setHeader("Cache-Control", "no-store"); response = client.execute(request);Anyone else had any success with this? Otherwise the platform is useless for me.
Gerry
Did you add both headers? Or just one at a time?
Kylar
Both headers at the same time.
Gerry
OK, I see the problem. First, you shouldn't add the same header twice, you should add it once with both values, and second, you should add it on the response object - That is to say the Server needs to add that header.
Kylar
So the solution is too change the server code that fulfills my request. This interesting, because this is not required on the blackberry or iphone. I suppose google could handle requests to the same url one of 2 ways and they choose the opposite of the other phone os companies.
Gerry