如何防止Android返回对我的HTTP请求的缓存响应?

Ger*_*rry 27 android http no-cache httprequest

我正在编写一个客户端,它正在对xml数据进行重复的http请求,这些请求随时间而变化.看起来Android堆栈正在缓存我的页面请求并重复返回同一页面.我如何确保每次都获得一个新页面?

- 代码---

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));
Run Code Online (Sandbox Code Playgroud)

谢谢,格里

Ric*_*dle 27

在URL的末尾附加未使用的参数:

HttpGet request = new HttpGet(url + "?unused=" + someRandomString());
Run Code Online (Sandbox Code Playgroud)

其中someRandomString()可能涉及当前时间.

它很粗糙,但无论外部因素如何,都可以使"正确"的解决方案失败,例如错误配置或错误的代理,它几乎可以保证工作.


Kyl*_*lar 25

添加HTTP标头:

Cache-Control: no-cache
Run Code Online (Sandbox Code Playgroud)

并看看是否有效.