Internet请求第一次运行,但不会在后续请求中运行

时间:2012-06-10 23:14:29

标签: java android

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

这是我的代码:

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

3 个答案:

答案 0 :(得分:0)

每次都重新创建URL对象。

答案 1 :(得分:0)

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

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

有关详细信息,请参阅The Android URLConnection documentation

答案 2 :(得分: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();

你需要抓住一些例外