Question

I am working on a little test app to scrape some data (in this case, XKCD comics) from the web and display it on my phone. This is my first foray into Android programming and I'm not too experienced with Java so I'm not being too ambitious. I successfully wrote a program in Java that performs the same function I want to do on Android and it works wonderfully, but some of my code that works in Java causes a force close in Android. Specifically this little code block:

try {
        home = Jsoup.connect("http://www.xkcd.com").get();
        Log.i("connect to home","completed");
    } catch (IOException e) {
        Log.i("connect to home","failed");
    }

Every time that runs, I see the "failed" message in the log. If I remove that section of my code, my app runs beautifully so I know the error must be there. "home" is defined as a Document elsewhere in my code, if you are wondering. In Java this runs fine, I also fond it strange that when in Eclipse developing for Android it forced me to surround that statement with a try-catch block but in Java I didn't need the try-catch. Any ideas why this won't work? If you need more information, or more of my code, I will provide it (although I tried to give the full story).

Thanks a lot for the help

Was it helpful?

Solution

Every time that runs, I see the "failed" message in the log.

Log the exception e. It contains information about how/why/where it failed. You should never suppress the exception without a very good reason (i.e. you know exactly what you're doing).

I also fond it strange that when in Eclipse developing for Android it forced me to surround that statement with a try-catch block but in Java I didn't need the try-catch.

Likely you already had a throws IOException on the method like so:

public void foo() throws IOException {
    Document document = Jsoup.connect(url).get();
    // ...
}

then you indeed don't need to put it in a try-catch.

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top