-3

I am trying to pull text from http://www.gasbuddy.com/GB_Price_List.aspx and set that text inside a TextView.

When I call This:

    try {
        Document document = Jsoup.connect(URL).get();

        // selector query
        Element gasPrice = ((Element) document).select(
                "table.listing > tbody > tr > td.p").first();
            Log.d("getData", gasPrice.text().toString());
            results = gasPrice.text().toString();
        } catch (Exception e) {
        Log.e("Darrell", "EXCEPTION IN DOINBACKGROUND()", e);
        e.printStackTrace();
    }

It always gets caught...

Here is the AsyncTask():

private class InternetGasBuddyConnection extends
        AsyncTask<String, String, String> {

    protected String doInBackground(String... arg0) {

        runOnUiThread(new Runnable() {
            public void run() {
                try {
                    Document document = Jsoup.connect(URL).get();

                    // selector query
                    Element gasPrice = ((Element) document).select(
                            "table.listing > tbody > tr > td.p").first();

                    Log.d("getData", gasPrice.text().toString());

                    results = gasPrice.text().toString();

                } catch (Exception e) {
                    Log.e("Darrell", "EXCEPTION IN DOINBACKGROUND()", e);
                    e.printStackTrace();
                }
            }
        });

        return results;
    }

    public void onPostExecute(String result) {
        travelCostView.setText(result);
    }
}

and this static final String URL = "http://www.gasbuddy.com/GB_Price_List.aspx"; is the URL I am trying to access

Logcat

07-22 14:20:56.941: W/IInputConnectionWrapper(8471): showStatusIcon on inactive InputConnection
07-22 14:21:02.497: W/ApplicationPackageManager(8471): getCSCPackageItemText()
07-22 14:21:02.587: D/Darrell(8471):  
07-22 14:21:02.607: E/Darrell(8471): EXCEPTION IN DOINBACKGROUND()
07-22 14:21:02.607: E/Darrell(8471): android.os.NetworkOnMainThreadException
07-22 14:21:02.607: E/Darrell(8471):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
07-22 14:21:02.607: E/Darrell(8471):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-22 14:21:02.607: E/Darrell(8471):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-22 14:21:02.607: E/Darrell(8471):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
07-22 14:21:02.607: E/Darrell(8471):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:439)
07-22 14:21:02.607: E/Darrell(8471):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
07-22 14:21:02.607: E/Darrell(8471):    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
07-22 14:21:02.607: E/Darrell(8471):    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
07-22 14:21:02.607: E/Darrell(8471):    at com.example.fuelcalculator.FuelEconomyCalculatorActivity$InternetGasBuddyConnection$1.run(FuelEconomyCalculatorActivity.java:300)
07-22 14:21:02.607: E/Darrell(8471):    at android.os.Handler.handleCallback(Handler.java:733)
07-22 14:21:02.607: E/Darrell(8471):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-22 14:21:02.607: E/Darrell(8471):    at android.os.Looper.loop(Looper.java:157)
07-22 14:21:02.607: E/Darrell(8471):    at android.app.ActivityThread.main(ActivityThread.java:5356)
07-22 14:21:02.607: E/Darrell(8471):    at java.lang.reflect.Method.invokeNative(Native Method)
07-22 14:21:02.607: E/Darrell(8471):    at java.lang.reflect.Method.invoke(Method.java:515)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-22 14:21:02.607: E/Darrell(8471):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-22 14:21:02.607: E/Darrell(8471):    at dalvik.system.NativeStart.main(Native Method)
07-22 14:21:02.607: W/System.err(8471): android.os.NetworkOnMainThreadException
07-22 14:21:02.607: W/System.err(8471):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
07-22 14:21:02.607: W/System.err(8471):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-22 14:21:02.607: W/System.err(8471):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-22 14:21:02.607: W/System.err(8471):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
07-22 14:21:02.607: W/System.err(8471):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
07-22 14:21:02.607: W/System.err(8471):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:439)
07-22 14:21:02.607: W/System.err(8471):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
07-22 14:21:02.607: W/System.err(8471):     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
07-22 14:21:02.607: W/System.err(8471):     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
07-22 14:21:02.607: W/System.err(8471):     at com.example.fuelcalculator.FuelEconomyCalculatorActivity$InternetGasBuddyConnection$1.run(FuelEconomyCalculatorActivity.java:300)
07-22 14:21:02.607: W/System.err(8471):     at android.os.Handler.handleCallback(Handler.java:733)
07-22 14:21:02.617: W/System.err(8471):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-22 14:21:02.617: W/System.err(8471):     at android.os.Looper.loop(Looper.java:157)
07-22 14:21:02.617: W/System.err(8471):     at android.app.ActivityThread.main(ActivityThread.java:5356)
07-22 14:21:02.617: W/System.err(8471):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 14:21:02.617: W/System.err(8471):     at java.lang.reflect.Method.invoke(Method.java:515)
07-22 14:21:02.617: W/System.err(8471):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-22 14:21:02.617: W/System.err(8471):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-22 14:21:02.617: W/System.err(8471):     at dalvik.system.NativeStart.main(Native Method)

I do have internet included in my Manifest so I know that is not the issue.

The BIG question here is... why, when I run the program and it gets to the above try-catch block in doInBackground() it is always caught.

Why will this code not work?

    Document document = Jsoup.connect(URL).get();

    // selector query
    Element gasPrice = ((Element) document).select(
            "table.listing > tbody > tr > td.p").first();
        Log.d("getData", gasPrice.text().toString());
        results = gasPrice.text().toString();
Darrell
  • 638
  • 4
  • 17

1 Answers1

1

While the comment to the question absolutely valid and points to the real problem, I'd still like to clarify why this happens. The thing is, JSoup's call Jsoup.connect(URL).get(); is a synchronous call. It blocks the calling thread until the data is fetched from the remote server. If you happen to make that call from some button click handler or on activity's startup, you'll face this error.

There are multiple techniques in Android to overcome this. Creating an AsyncTask instance is definitely one of those, because it's not only takes care about making the call in the background (using a SingleThreadExecutor), but also makes sure that results of your parsing will be delivered to the UI thread (it's important since you most probably want to display them to the user). You can find information on how to implement AsyncTask correctly here: http://developer.android.com/reference/android/os/AsyncTask.html

Haspemulator
  • 11,050
  • 9
  • 49
  • 76