0

我希望有人可以帮助我,因为我一直在寻找它,但没有找到任何有用的东西。

我正在连接到列表中的多个 url,一切正常,但后来我开始在某些上收到 404 错误,因此现在我想捕获该错误,以便程序不会终止并继续遍历列表 url。

这是我得到的错误

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

我正在使用 Jsoup.connect 并且错误是在这行代码中引起的

    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();

如何更改代码以便获得状态代码。我已经尝试过 Connection.response (我在这个网站上找到的东西作为此类问题的解决方案)但我遇到了投射错误

 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){

但我收到以下错误

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;)

任何帮助将不胜感激,谢谢。

4

1 回答 1

1

您的代码中有一个错字:

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

Response是一个静态类(接口是正确的)Connection,所以只需更改您的代码:

Connection.Response response = Jsoup.connect(...) ...
于 2014-04-14T15:20:26.343 回答