Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 800
Gerry Avatar asked Apr 22 '10 16:04

Gerry


People also ask

Which HTTP request will never get cached?

A proxy HTTP server that forwards your message to the server would never cache anything but a GET or a HEAD request.

How does HTTP cache work android?

To measure cache effectiveness, this class tracks three statistics: Request Count: the number of HTTP requests issued since this cache was created. Network Count: the number of those requests that required network use. Hit Count: the number of those requests whose responses were served by the cache.

Can HTTP control caching?

The Cache-Control HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g. Proxies, CDNs).

What is HTTP response cache?

android.net.http.HttpResponseCache. Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth. This class supports HttpURLConnection and HttpsURLConnection ; there is no platform-provided cache for DefaultHttpClient or AndroidHttpClient .


2 Answers

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.

like image 182
RichieHindle Avatar answered Oct 10 '22 17:10

RichieHindle


add a HTTP header:

Cache-Control: no-cache 

and see if that works.

like image 42
Kylar Avatar answered Oct 10 '22 18:10

Kylar