Search code examples
androidjsoup

Jsoup, connection.excute(), connection.get() is null


I'm developing an android app by crawling HTML from an Web site but I got NullPointerException when I'm using connection.get() to get Document. So I tested like this.

try {
    conn = Jsoup.connect(url);
    document = conn.get();
    res = conn.execute();
} catch (IOException e) {
    e.printStackTrace();
}

if (conn != null) {
    textView.append("conn is not null\n");
} else {
    textView.append("conn is null\n");
}

if (document != null) {
    textView.append("document not is null\n");
} else {
    textView.append("document is null\n");
}

if (res != null) {
    textView.append("res is not null.\n");
} else {
    textView.append("res is null\n");
}

and the result is

conn is not null.
document is null.
res is null.

I tried this on http://google.com and the result is

conn is not null.
document is not null.
res is not null.

and I can get Document, of course.

Why do I get null from connection.excute(), connection.get()?


Solution

  • First of all looking at the documentation, you don't need to call either execute and get: the connection.get() method

    Execute the request as a GET, and parse the result.

    The reason why you don't get any response from your url it's because you try to run on main thread