0

I have a problem with parsing html with JSoup

The code is from a tutorial found here, but it just wont work it keeps trowing error

Here is the code

static final String BLOG_URL = "http://xjaphx.wordpress.com/";

@Override
public void onCreate(Bundle savedInstanceState) {
    // set layout view
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    // process
   try {
        ((TextView)findViewById(R.id.text)).setText(getBlogStats());
        Log.i("complete","completed");
    } catch (Exception ex) {
        ((TextView)findViewById(R.id.text)).setText("Error");
         Log.i("error","error", ex);
    }
}



protected String getBlogStats() throws Exception {
    String result = "";
    // get html document structure
    Document document = Jsoup.connect(BLOG_URL).get();
    // selector query
    Elements nodeBlogStats = document.select("div#blog-stats ul li");
    // check results
    if(nodeBlogStats.size() > 0) {
        // get value
        result = nodeBlogStats.get(0).text();
    }

    // return
    return result;
}
    }

I have also tryed to log error but i dont understand anything from the output

05-18 15:18:19.706: V/Performance(28824): >>AppBindData{appInfo=ApplicationInfo{40d7a918 com.android.test}} handleBindApplication
05-18 15:18:19.746: V/Performance(28824): >>android.app.ActivityThread@40d792d0 newApplication
05-18 15:18:19.746: V/Performance(28824): <<android.app.ActivityThread@40d792d0 newApplication, 0ms
05-18 15:18:19.746: V/Performance(28824): >>android.app.Application@40d82028 onCreate
05-18 15:18:19.746: V/Performance(28824): <<android.app.Application@40d82028 onCreate, 0ms
05-18 15:18:19.746: V/Performance(28824): <<AppBindData{appInfo=ApplicationInfo{40d7a918 com.android.test}} handleBindApplication, 35ms
05-18 15:18:19.746: V/Performance(28824): >>com.android.test.testActivity@40d83970 onCreate
05-18 15:18:19.876: I/error(28824): error
05-18 15:18:19.876: I/error(28824): android.os.NetworkOnMainThreadException
05-18 15:18:19.876: I/error(28824):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)
05-18 15:18:19.876: I/error(28824):     at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
05-18 15:18:19.876: I/error(28824):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
05-18 15:18:19.876: I/error(28824):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-18 15:18:19.876: I/error(28824):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
05-18 15:18:19.876: I/error(28824):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:404)
05-18 15:18:19.876: I/error(28824):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:391)
05-18 15:18:19.876: I/error(28824):     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:157)
05-18 15:18:19.876: I/error(28824):     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:146)
05-18 15:18:19.876: I/error(28824):     at com.android.test.testActivity.getBlogStats(testActivity.java:42)
05-18 15:18:19.876: I/error(28824):     at com.android.test.testActivity.onCreate(testActivity.java:29)
05-18 15:18:19.876: I/error(28824):     at android.app.Activity.performCreate(Activity.java:4531)
05-18 15:18:19.876: I/error(28824):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
05-18 15:18:19.876: I/error(28824):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
05-18 15:18:19.876: I/error(28824):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
05-18 15:18:19.876: I/error(28824):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
05-18 15:18:19.876: I/error(28824):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
05-18 15:18:19.876: I/error(28824):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 15:18:19.876: I/error(28824):     at android.os.Looper.loop(Looper.java:154)
05-18 15:18:19.876: I/error(28824):     at android.app.ActivityThread.main(ActivityThread.java:4944)
05-18 15:18:19.876: I/error(28824):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 15:18:19.876: I/error(28824):     at java.lang.reflect.Method.invoke(Method.java:511)
05-18 15:18:19.876: I/error(28824):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-18 15:18:19.876: I/error(28824):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-18 15:18:19.876: I/error(28824):     at dalvik.system.NativeStart.main(Native Method)
05-18 15:18:19.876: V/Performance(28824): <<com.android.test.testActivity@40d83970 onCreate, 128ms
05-18 15:18:19.876: V/Performance(28824): >>com.android.test.testActivity@40d83970 onStart
05-18 15:18:19.876: V/Performance(28824): <<com.android.test.testActivity@40d83970 onStart, 0ms
05-18 15:18:19.876: V/Performance(28824): >>com.android.test.testActivity@40d83970 onPostCreate
05-18 15:18:19.876: V/Performance(28824): <<com.android.test.testActivity@40d83970 onPostCreate, 1ms
05-18 15:18:19.876: V/Performance(28824): >>com.android.test.testActivity@40d83970 onResume
05-18 15:18:19.876: V/Performance(28824): <<com.android.test.testActivity@40d83970 onResume, 0ms
05-18 15:18:19.936: D/libEGL(28824): loaded /system/lib/egl/libGLES_android.so
05-18 15:18:19.936: D/libEGL(28824): loaded /system/lib/egl/libEGL_adreno200.so
05-18 15:18:19.946: D/libEGL(28824): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-18 15:18:19.946: D/libEGL(28824): loaded /system/lib/egl/libGLESv2_adreno200.so
05-18 15:18:20.027: D/OpenGLRenderer(28824): Enabling debug mode 0
1
  • Just because you don't understand the error output (yet), doesn't mean we don't either! Post it here, it usually contians useful information. Commented May 18, 2012 at 13:16

1 Answer 1

1

Since you have not posted the error logs, the following is a result of guess work.

You are connecting to the server and parsing the HTML in the main/UI thread. This will make the main thread unresponsive and get terminated eventually (or a NetworkOnMainThreadException thrown). Take a look at Painless Threading to understand how you can perform a long duration task in the background and update the UI when it is complete.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry but im a beginer in android development, and i have followed tutorial to make this, im still learning, im not sure that i will understand that. I have added logcat outout
Your logcat output verifies my guess. You are indeed getting a NetworkOnMainThreadException. If you don't know how to use an AsyncTask, now will be the time. Do a little bit of research, search the web and try things out yourself. Isn't that how you learn?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.