我使用此代碼中檢索HTML頁面並解析它如何避免強制關閉IOException和SocketTimeoutException JSoup?
while(doc == null && retry<5){
retry++;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
Log.e("ReleaseInfo", "JSoup get didnt get a document", e);
}
一旦文檔檢索我使用這得到NULL的含量的一些信息和測試。
overview = doc.select("div#object-overview").last();
if(overview != null){
paragraph = overview.select("p").last();
if(paragraph != null){
Log.v("Paragraph", paragraph.text());
}else{
Toast.makeText(releaseInfo.this, "No over view content", Toast.LENGTH_SHORT);
}
}
else{
}
featureList = doc.select("div.callout-box").last();
if(featureList != null){
Log.v("OverView", featureList.text());
正如你所看到的,我用日誌語句捕捉錯誤。 IOException異常告訴我這個
08-17 12:54:11.535: ERROR/ReleaseInfo(5960): JSoup get didnt get a document
08-17 12:54:11.535: ERROR/ReleaseInfo(5960): java.net.SocketTimeoutException
08-17 12:54:11.535: ERROR/ReleaseInfo(5960): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564)
08-17 12:54:11.535: ERROR/ReleaseInfo(5960): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
08-17 12:54:11.535: ERROR/ReleaseInfo(5960): at java.io.InputStream.skip(InputStream.java:258)
08-17 12:54:11.535: ERROR/ReleaseInfo(5960): at org.apache.harmony.luni.net.SocketInputStream.skip(SocketInputStream.java:93)
08-17 12:54:11.535: ERROR/ReleaseInfo(5960): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.skipOutstandingChunks(HttpURLConnectionImpl.java:374)
我如何才能讓我的代碼的防禦反對這種錯誤時不強制關閉文檔時心不是retreived?
另外我想趕上並回應這個異常,導致我的應用程序強制關閉。
08-17 16:27:29.245: ERROR/ReleaseInfo(8641): JSoup get didnt get a document
08-17 16:27:29.245: ERROR/ReleaseInfo(8641): java.io.IOException: 404 error loading URL http://pc.gamespy.com/web-games/parking-wars-2/
08-17 16:27:29.245: ERROR/ReleaseInfo(8641): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:387)
08-17 16:27:29.245: ERROR/ReleaseInfo(8641): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
08-17 16:27:29.245: ERROR/ReleaseInfo(8641): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
08-17 16:27:29.245: ERROR/ReleaseInfo(8641): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
的Java .... ??????? – yoshi24
如果你能提供幫助,那將會很棒,如果不是你的評論不需要的話。並且將被標記爲 – yoshi24