我目前正试图访问大量非常类似的网站,它们只包含文本,并且都以相同的方式进行格式化。BufferedReader故障排除Java
我第一次调用5-30次这个方法,它可以工作,但是之后它返回null。没有错误。有没有理由不能得到字符串?
经过一段文字插入后,我发现随机地看起来,line = in.readLine()
是null
,它跳过了字符串抓取的主体。我非常不使用BufferedReader
,所以很可能是问题所在。如果您有任何提示或可以解决此问题的方法,将不胜感激。
public static String pullString(int id) throws IOException {
String price;
logError("Checkpoint 1");
URL url = new URL("example.com");
URLConnection con = url.openConnection();
logError("Checkpoint 2");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
logError("Checkpoint 3");
String line;
while ((line = in.readLine()) != null) {
// ^for some reason this becomes null, but on identical pages it works fine.
//Removed unneeded info
return ---;
} catch (NumberFormatException e) {
logError("NumberFormatException");
return null;
}
}
}
logError("left out the back");
return null;
}
代码中的“try”在哪里?我看到一个“catch”,但没有“try”。 – Mysticial