0

我是 android 新手,我正在尝试使用 jsoup 从网站返回数据,但是当我执行 get() 方法时出现异常。这是我的代码:

public void Iqama (View view) throws IOException
{
    Document doc = Jsoup.connect("http://google.com/").get();

}

这是我的日志猫

06-07 12:59:38.428: E/AndroidRuntime(2281): FATAL EXCEPTION: main

06-07 12:59:38.428: E/AndroidRuntime(2281): java.lang.IllegalStateException: Could not execute method of the activity
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.view.View$1.onClick(View.java:3735)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.view.View.performClick(View.java:4354)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.view.View$PerformClick.run(View.java:17948)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.os.Handler.handleCallback(Handler.java:725)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.os.Looper.loop(Looper.java:137)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.app.ActivityThread.main(ActivityThread.java:5293)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at java.lang.reflect.Method.invoke(Method.java:511)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at dalvik.system.NativeStart.main(Native Method)
06-07 12:59:38.428: E/AndroidRuntime(2281): Caused by: java.lang.reflect.InvocationTargetException
06-07 12:59:38.428: E/AndroidRuntime(2281):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at java.lang.reflect.Method.invoke(Method.java:511)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.view.View$1.onClick(View.java:3730)
06-07 12:59:38.428: E/AndroidRuntime(2281):     ... 11 more
06-07 12:59:38.428: E/AndroidRuntime(2281): Caused by: android.os.NetworkOnMainThreadException
06-07 12:59:38.428: E/AndroidRuntime(2281):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
06-07 12:59:38.428: E/AndroidRuntime(2281):     at rasha.example.masjed.Second.Iqama(Second.java:35)
06-07 12:59:38.428: E/AndroidRuntime(2281):     ... 14 more
06-07 12:59:51.831: I/Process(2281): Sending signal. PID: 2281 SIG: 9
06-07 12:59:52.151: D/libEGL(3235): loaded /system/lib/egl/libEGL_adreno200.so
06-07 12:59:52.151: D/libEGL(3235): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
06-07 12:59:52.151: D/libEGL(3235): loaded /system/lib/egl/libGLESv2_adreno200.so
06-07 12:59:52.161: I/Adreno200-EGL(3235): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build:  (CL3544079)
06-07 12:59:52.161: I/Adreno200-EGL(3235): Build Date: 03/28/13 Thu
06-07 12:59:52.161: I/Adreno200-EGL(3235): Local Branch: adreno_20130328
06-07 12:59:52.161: I/Adreno200-EGL(3235): Remote Branch: 
06-07 12:59:52.161: I/Adreno200-EGL(3235): Local Patches: 
06-07 12:59:52.161: I/Adreno200-EGL(3235): Reconstruct Branch: 
06-07 12:59:52.201: D/OpenGLRenderer(3235): Enabling debug mode 0
06-07 13:00:22.140: W/IInputConnectionWrapper(3235): setComposingText on inactive InputConnection

这是我的 Mainfest 文件

    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

并在清单中的应用程序标记之前使用此权限

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

我在 libs 文件夹中使用 JSOUP-1.7.2.jar

4

3 回答 3

1

导致崩溃的错误是 NetworkOnMainThreadException。您在主 UI 线程上调用网络函数。您应该使用 AsyncTask。请参阅此处了解更多信息。

于 2013-06-07T18:19:08.510 回答
0

问题是Jsoup.connect("http://google.com/").get();需要很多时间,并且不允许在 UI 线程中发生。

android.os.NetworkOnMainThreadException

看看AsyncTask来解决你的问题。

就像是:

private class JsoupTask extends AsyncTask<Void, Void, Document> {
   protected Document doInBackground(Void... nothing) {
       Document doc;
       doc = Jsoup.connect("http://google.com/").get();
       return doc;
   }


   protected void onPostExecute(Document doc) {
       // do something with doc
   }
}

要运行它:

new JsoupTask().execute();
于 2013-06-07T18:20:37.513 回答
0

使用AsyncTask( API Doc ) 并删除android:targetSdkVersion="16" />.

于 2013-06-08T06:52:00.930 回答