I am currently trying to access a multitude of very similar websites that contain only text, and are all formatted in the same way.
The first 5-30 times I call this method, it works, but after that it returns null. No error. Is there any reason that it would not be able to get the string?
After a bit of text inserting, I found that randomly, it seems, line = in.readLine()
is null
, and it skips the body of the string grabbing. I don't use BufferedReader
very much, so it could well be the problem. If you have any tips, or ways I could troubleshoot this, It would be greatly appreciated.
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;
}