Question

I hope someone can help me with this as I have been searching for it but didn't find anything working.

I am connecting to a number of urls from a list and everything works fine but then I started getting a 404 error on some hence now I want to catch the error so that the program doesn't terminate and keeps going through the list urls.

This is the error I got

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=http:nameofthesite

I am using Jsoup.connect and the error is caused in this line of code

    Document doc= Jsoup.connect(countryUrl[i2]).timeout(10*1000)                      
    .userAgent("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5")
    .get();

How can I change the code so that I can get the status code. I've tried Connection.response (something I found on this site as a solution for this sort of problem) but I was getting casting error

 Connection.response  response= Jsoup.connect(countryUrl[i2]).timeout(10*1000)                      
            .userAgent("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5")
            .execute();
            int statusCode = response.statusCode();
            if(statusCode==200){

but I get the following error

groovy.lang.MissingMethodException: No signature of method: static org.jsoup.Connection.response() is applicable for argument types: (org.jsoup.helper.HttpConnection$Response) values: [org.jsoup.helper.HttpConnection$Response@c7325ae]
Possible solutions: respondsTo(java.lang.String), respondsTo(java.lang.String, [Ljava.lang.Object;)

any help will be appreciated, thanks.

Was it helpful?

Solution

There's a typo in your code:

Connection.response response = Jsoup.connect(...) ...
//         ^
//         |

Response is a static class (interface to be correct) of Connection, so just change your code:

Connection.Response response = Jsoup.connect(...) ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top