网络中的UI线程中的机器人装置(机器人3.0或更高版本)(Network in UI thread

2019-10-16 18:16发布

我要解析一个网页的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版本的问题,以我的标题为别人更好的搜索

Answer 1:

在Android 3.0的和新的,你从接触网络或UI线程上的文件IO取缔。 这是因为它强迫你不能中断的,可能需要很长一段时间的操作,用户界面是一件好事。 看这句话在你的日志:

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

你已经想通了其中的异常被抛出,现在你需要把这些代码在的doInBackground()方法的AsyncTask 。



文章来源: Network in UI thread in an android device(android 3.0 or newer)