Android:使用 BufferedReader 提取网站:提取的信息已过时
Android: extracting website using BufferedReader: Information extracted are outdated
我正在使用以下代码阅读 html 网站的 public 源代码:
代码:
@Override
protected Void doInBackground(Void... params)
{
try
{
URL url = new URL(""+URL);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
PageCode = "";
OriginalPageCode = "";
while ((inputLine = in.readLine()) != null)
{
PageCode += inputLine;
}
OriginalPageCode = PageCode;
try
{
extract_website_and_save(); // extracting data from PageCode
}
catch (Exception e1)
{
}
in.close();
}
背景:
以上代码有时可以正确获取最新的网站。但有时它link访问过时的网站版本,因此无法获取网站的最新信息。
我很好奇为什么会出现上述情况,是否与从缓存中提取而不是真正更新的网站有关??
因此我用Chrome浏览同一个link,发现Chrome也抓取了过时的网站。
我已尝试重启设备,但问题仍然存在。
30 分钟到一个小时后,我请求应用程序再次获取,然后它可以提取最新的信息。我同时使用Chrome浏览网站,Chrome现在可以获得最新的网站
问题:
上面的BufferedReader应该和Chrome没有关系吧?但它们遵循相同的逻辑,因此从缓存中提取而不是从最新的网站中提取?
我强烈怀疑端点被 URL
缓存
尝试这样的事情
urlSrt = urlSrt + "?x=" + new Random().nextInt(100000);
// If your URL already is passing parameters i.e. example.com?x=1&p=pass - then modify
// the urlSrt line to to use an "&" and not "?"
// i.e. urlSrt = urlSrt + "&x=" + new Random().nextInt(100000);
URL url = new URL(urlSrt);
URLConnection con = url.openConnection();
con.setUseCaches(false); //This will stop caching!
因此,如果您将代码修改为类似这样的内容。
URLConnection con = url.openConnection();
con.setUseCaches(false);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
我正在使用以下代码阅读 html 网站的 public 源代码:
代码:
@Override
protected Void doInBackground(Void... params)
{
try
{
URL url = new URL(""+URL);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
PageCode = "";
OriginalPageCode = "";
while ((inputLine = in.readLine()) != null)
{
PageCode += inputLine;
}
OriginalPageCode = PageCode;
try
{
extract_website_and_save(); // extracting data from PageCode
}
catch (Exception e1)
{
}
in.close();
}
背景:
以上代码有时可以正确获取最新的网站。但有时它link访问过时的网站版本,因此无法获取网站的最新信息。
我很好奇为什么会出现上述情况,是否与从缓存中提取而不是真正更新的网站有关??
因此我用Chrome浏览同一个link,发现Chrome也抓取了过时的网站。
我已尝试重启设备,但问题仍然存在。
30 分钟到一个小时后,我请求应用程序再次获取,然后它可以提取最新的信息。我同时使用Chrome浏览网站,Chrome现在可以获得最新的网站
问题:
上面的BufferedReader应该和Chrome没有关系吧?但它们遵循相同的逻辑,因此从缓存中提取而不是从最新的网站中提取?
我强烈怀疑端点被 URL
缓存尝试这样的事情
urlSrt = urlSrt + "?x=" + new Random().nextInt(100000);
// If your URL already is passing parameters i.e. example.com?x=1&p=pass - then modify
// the urlSrt line to to use an "&" and not "?"
// i.e. urlSrt = urlSrt + "&x=" + new Random().nextInt(100000);
URL url = new URL(urlSrt);
URLConnection con = url.openConnection();
con.setUseCaches(false); //This will stop caching!
因此,如果您将代码修改为类似这样的内容。
URLConnection con = url.openConnection();
con.setUseCaches(false);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));