我正在尝试使用jsoup从网站返回数据,但是当我执行get()方法时会出现异常

时间:2013-06-07 18:13:53

标签: android jsoup

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

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

}

这是我的logcat

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

3 个答案:

答案 0 :(得分:1)

导致崩溃的错误是NetworkOnMainThreadException。您在主Ui线程上调用网络功能。你应该使用AsyncTask。有关详细信息,请参阅here

答案 1 :(得分: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();

答案 2 :(得分:0)

使用AsyncTaskAPI Doc)并移除android:targetSdkVersion="16" />