如何避免使用 IOException 和 SocketTimeoutException JSoup 强制关闭?

发布于 2024-11-30 06:28:07 字数 2814 浏览 1 评论 0原文

我正在使用此代码来检索 html 页面并解析它。

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


                }

一旦检索到文档,我将使用它来获取一些信息并测试是否为空。

  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)

如何让我的代码防御此错误,以便在未检索文档时不强制关闭?

另外,我想捕获并响应此异常,导致我的应用程序强制关闭。

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)

I am using this code to retreive a html page and parse it

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


                }

Once the doc is retrieved i am using this to get some information and testing for nullness.

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

As you see i am catching the error with a log statement. The IOException is telling me this

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)

How could i make my code defensive against this error to not force close when the document isnt retreived?

Also i would like to catch and respond to this exception that causes my application to force close.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖心男生 2024-12-07 06:28:07

您显示的日志条目表明您捕获了异常并将其作为错误打印到日志中,正如您所期望的那样。这些条目都不应该强制关闭您的应用程序。还有其他条目吗?例如,如果重试 5 次后您仍然没有建立连接,则“doc”将等于 null,并且以下行将导致未捕获的 nullPointerException,然后将关闭您的应用程序:

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

The log entries you are showing indicate that you caught the exception and printed it to the Log as an error, as you would expect it to. Neither of those entries should force close your app. Are there any other entries? For example if after your 5 retries you still have not made a connection, than 'doc' will equal null and the following line will cause an uncaught nullPointerException would then will bring down your app:

overview = doc.select("div#object-overview").last();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文