1

我必须解析网站 html 代码并在我的应用程序中显示一些信息
所以我尝试 JSoup 来获取结果
它在模拟器上运行良好,但在真实设备中它会崩溃;这是日志:

05-25 21:42:20.935: D/AndroidRuntime(32230): Shutting down VM  
05-25 21:42:20.935: W/dalvikvm(32230): threadid=1: thread exiting with uncaught exception (group=0x401b9760)  
05-25 21:42:20.940: E/AndroidRuntime(32230): FATAL EXCEPTION: main
05-25 21:42:20.940: E/AndroidRuntime(32230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abbasizadeh.Currency/com.abbasizadeh.Currency.CurrencyUpdatableActivity}: android.os.NetworkOnMainThreadException
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.os.Looper.loop(Looper.java:132)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.app.ActivityThread.main(ActivityThread.java:4123)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at java.lang.reflect.Method.invoke(Method.java:491)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at dalvik.system.NativeStart.main(Native Method)
05-25 21:42:20.940: E/AndroidRuntime(32230): Caused by: android.os.NetworkOnMainThreadException
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at java.net.InetAddress.getAllByName(InetAddress.java:249)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:404)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:391)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:157)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:146)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at com.abbasizadeh.Currency.CurrencyUpdater.updateInfo(CurrencyUpdater.java:35)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at com.abbasizadeh.Currency.CurrencyUpdatableActivity.onCreate(CurrencyUpdatableActivity.java:20)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-25 21:42:20.940: E/AndroidRuntime(32230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
05-25 21:42:20.940: E/AndroidRuntime(32230):    ... 11 more  

CurrencyUpdatableActivity.java:20我打电话时CurrencyUpdater.java:35CurrencyUpdater.java:35是这样的:

Document document = Jsoup.connect(url).get();

感谢您在这个奇怪问题上的帮助

编辑:我在标题中添加了 android 版本问题,以便更好地搜索其他人

4

1 回答 1

2

在 Android 3.0 及更高版本中,您被禁止在 UI 线程上触摸网络或文件 IO。这是一件好事,因为它迫使您不要中断用户界面以进行可能需要很长时间的操作。查看日志中的这一行:

05-25 21:42:20.940: E/AndroidRuntime(32230): Caused by: android.os.NetworkOnMainThreadException

您已经弄清楚异常是在哪里引发的,现在您需要将该代码放入AsyncTask的 doInBackground() 方法中。

于 2012-05-25T22:31:24.377 回答