Domanda

Spero che qualcuno possa aiutarmi con questo come lo sto cercando, ma non ho trovato nulla di lavorare.

Sto connettendo a un numero di URL da un elenco e tutto funziona bene, ma poi ho iniziato a ottenere un errore 404 su alcuni quindi ora voglio catturare l'errore in modo che il programma non terminasse e continua ad andare attraverso l'elencoURL.

Questo è l'errore che ho

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

Sto usando jsoup.connect e l'errore è causato in questa linea di codice

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

Come posso modificare il codice in modo da poter ottenere il codice di stato. Ho provato Connection.Response (qualcosa che ho trovato su questo sito come soluzione per questo tipo di problema) ma stavo ricevendo errori di lancio

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

Ma ottengo il seguente errore

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

Qualsiasi aiuto sarà apprezzato, grazie.

È stato utile?

Soluzione

C'è un typo nel tuo codice:

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

Response è una classe statica (interfaccia da correggere) di Connection, quindi basta cambiare il tuo codice:

Connection.Response response = Jsoup.connect(...) ...
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top