English 中文(简体)
用户界面线和机器人装置(和3.0机件或更新机件)中的UI线网络
原标题:Network in UI thread in an android device(android 3.0 or newer)

I have to parse a website html code and show some info in my app
So I tried JSoup for getting result
It works great on the emulator but in a real device it will crashes; here is the log :

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  

in CurrencyUpdatableActivity.java:20 I call CurrencyUpdater.java:35 and CurrencyUpdater.java:35 is this:

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

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

编辑:为了更好地搜索其他版本,我在我的标题中添加了机器人版本问题

最佳回答

Inandroid 3. 0 和 newer, 您被禁止在 UI 线索上触摸网络或文件 IO 。 这是件好事, 因为它迫使您不中断用户界面的操作, 操作可能需要很长时间。 请查看您的日志中的这一行 :

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

您已经知道该例外被扔到哪里了, 现在您需要将该代码放入 < a href=>的 DOInBackground () 方法“ http:// developmenter.android. com/ reference/android/ os/ AsyncTask. html" rel=“ no follow” > Asynctask 。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签