在Android中使用Jsoup解析HTML时强制关闭

时间:2014-03-27 08:24:18

标签: android html jsoup

我在Android中遇到了2天的问题.. 我无法从URL解析HTML页面.. 我尝试了很多方法,并且我为简单的方法获得了Jsoup .. 这是我的剧本:

TextView tv = (TextView) findViewById(R.id.tvv);

    Document doc;
    try {
        doc = Jsoup.connect("http://google.com").get();
        String title = doc.title();
    } catch (IOException e) {
        e.printStackTrace();
    }

但是,当我运行脚本时,它会强制关闭..我只是不知道问题是什么。 我在这里看到了教程:http://jsoup.org/cookbook/input/load-document-from-url

这是Logcat所说的:

03-27 15:29:39.032: E/AndroidRuntime(5046): FATAL EXCEPTION: main
03-27 15:29:39.032: E/AndroidRuntime(5046): java.lang.RuntimeException: Unable to start activity ComponentInfo{pt.tarie.cookiesapps/pt.tarie.cookiesapps.HTMLParse}: android.os.NetworkOnMainThreadException
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.os.Looper.loop(Looper.java:137)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.ActivityThread.main(ActivityThread.java:5099)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at java.lang.reflect.Method.invoke(Method.java:511)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at dalvik.system.NativeStart.main(Native Method)
03-27 15:29:39.032: E/AndroidRuntime(5046): Caused by: android.os.NetworkOnMainThreadException
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:439)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at pt.tarie.cookiesapps.HTMLParse.onCreate(HTMLParse.java:23)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.Activity.performCreate(Activity.java:5117)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
03-27 15:29:39.032: E/AndroidRuntime(5046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

发布您的堆栈跟踪.U无法在UI Thread中执行网络操作。确保您在asyc任务中运行