How do I do error handling on Jsoup when the program fails to connect to the website?
For example that the website doesn\'t exist and I would like to print error message
Try this one,
try{
Connection.Response response = Jsoup.connect("https://asdasdasd.com")
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.ignoreHttpErrors(true).
.execute();
int statusCode = response.statusCode();
if(statusCode == 200) {
Document doc = Jsoup.connect("https://asdasdasd.com").get();
Elements links = doc.select("div.postdetails");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("div"));
System.out.println("text : " + link.text());
}
}
else {
System.out.println("received error code : " + statusCode);
}
} catch (IOException e) {
e.printStackTrace();
}