2011-08-17 66 views
1

我使用此代碼中檢索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) 
+0

的Java .... ??????? – yoshi24

+0

如果你能提供幫助,那將會很棒,如果不是你的評論不需要的話。並且將被標記爲 – yoshi24

回答

4

您顯示的日誌條目表明您發現了異常,並將其作爲錯誤打印到日誌中,正如您所期望的那樣。這些條目都不應該強制關閉您的應用程序。還有其他條目嗎?例如,如果你試5次後,你還沒有建立連接,不是「DOC」將等於空,下面的行會引起那麼未捕獲NullPointerException異常就會將降低您的應用程序:

overview = doc.select("div#object-overview").last(); 
+0

這是問題。所以我做的是包圍overview = doc.select(「div#object-overview」)。last();與if(doc!= null){並且做到了。它現在沒有強行關閉它就記錄了錯誤。 – yoshi24

+0

感謝您的幫助! – yoshi24

+0

這也幫助了我,處理異常是一回事,但考慮事後要做什麼是一個有趣的問題,讓他們重新嘗試或回到最後一個視圖。 – Neil