0

我试图弄清楚 JSOUP,所以我找到了一个非常简单的例子,无论我做什么,我都会得到这个错误:

我有 importet: "jsoup-1.7.2.jar" 到 /libs

设置上网权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

主要活动:

package iha.itsmap.jsoup2;

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Document doc;
        try {
            doc = Jsoup.connect("http://google.com/").get();
            String title = doc.title();
            System.out.print(title);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

错误是这样的:

05-28 11:20:12.873: W/dalvikvm(7142): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-28 11:20:12.903: E/AndroidRuntime(7142): FATAL EXCEPTION: main
05-28 11:20:12.903: E/AndroidRuntime(7142): java.lang.RuntimeException: Unable to start activity ComponentInfo{iha.itsmap.jsoup2/iha.itsmap.jsoup2.MainActivity}: android.os.NetworkOnMainThreadException
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.os.Looper.loop(Looper.java:137)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at java.lang.reflect.Method.invoke(Method.java:511)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at dalvik.system.NativeStart.main(Native Method)
05-28 11:20:12.903: E/AndroidRuntime(7142): Caused by: android.os.NetworkOnMainThreadException
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at iha.itsmap.jsoup2.MainActivity.onCreate(MainActivity.java:18)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.Activity.performCreate(Activity.java:5104)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-28 11:20:12.903: E/AndroidRuntime(7142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-28 11:20:12.903: E/AndroidRuntime(7142):     ... 11 more

谁能告诉为什么会发生这个错误?

如果我注释掉这段代码:

//  try {
//      doc = Jsoup.connect("http://google.com/").get();
//      String title = doc.title();
//      System.out.print(title);
//  } catch (IOException e) {
//      // TODO Auto-generated catch block
//      e.printStackTrace();
//  }

该应用程序不会崩溃,但我似乎仍然收到此错误,我不知道它是否重要

05-28 11:13:37.484: E/Trace(6996): error opening trace file: No such file or directory (2)
4

1 回答 1

1

看来您使用的是 Android 3.0+。在 Honeycomb 或更高版本上的 UI 线程上的网络访问会导致android.os.NetworkOnMainThreadException.

使用AsyncTask在您的用户界面上执行异步工作。

于 2013-05-28T11:55:19.737 回答