0

我在使用 JSoup 解析 html 时遇到问题

该代码来自 此处找到的教程,但它只是无法正常工作,它会不断出现错误

这是代码

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;
}
    }

我也尝试记录错误,但我不了解输出中的任何内容

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
4

1 回答 1

1

由于您尚未发布错误日志,以下是猜测工作的结果。

您正在连接到服务器并在主/UI 线程中解析 HTML。这将使主线程无响应并最终终止(或NetworkOnMainThreadException抛出)。查看Painless Threading以了解如何在后台执行长时间任务并在完成后更新 UI。

于 2012-05-18T13:18:00.133 回答