2

在第一次请求时,它会进入互联网并检索数据。当我再次尝试时,它只会给我输入流中已经存在的数据。如何清除缓存的数据,以便每次都执行新请求?

这是我的代码:

InputStreamReader in = null;
in = new InputStreamReader(url.openStream());
response = readFully(in);
url.openStream().close();
4

3 回答 3

0

您可以创建 URLConnection 并显式设置缓存策略:

final URLConnection conn = (URLConnection)url.openConnection();
conn.setUseCaches(false); // Don't use a cached copy.
InputStream in = new BufferedInputStream(urlConnection.getInputStream());

有关更多详细信息,请参阅Android URLConnection 文档

于 2012-06-10T23:27:19.703 回答
0

HttpClient API 可能有用

address = "http://google.com"
String html = "";
HttpClient httpClient = DefaultHttpClient();
HttpGet httpget = new HttpGet(address);
BufferedReader in = null;
HttpResponse response = null;
response = httpClient.execute(httpget);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.seperator");
while ((l = in.readLine()) != null) {
    sb.append(l + nl);
}
in.close();
html = sb.toString();

您需要捕获一些异常

于 2012-06-10T23:34:36.413 回答
0

每次重新创建 URL 对象。

于 2012-06-10T23:16:01.483 回答