1

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)
yoshi24
  • 3,147
  • 8
  • 45
  • 62

1 Answers1

4

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();
Gregg Rivinius
  • 775
  • 1
  • 8
  • 13
  • that was the problem. so what I did was surrounded the overview = doc.select("div# object-overview").last(); with if(doc! = null){ and that did it. it logged the error now without force closing it. – yoshi24 Aug 17 '11 at 21:18
  • This also helped me, handling exceptions is one thing, but thinking about what to do afterwards is an interesting problem, offer them a re-try or just go back to the last view. – Neil Dec 17 '12 at 09:51