码迷,mamicode.com
首页 > 移动开发 > 详细

android网络访问异常:android.os.NetworkOnMainThreadException

时间:2014-12-13 18:02:21      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:android   style   http   io   ar   color   os   sp   java   

 Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:388)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
            at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
            at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
            at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
            at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
            at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
            at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
            at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)

            at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)

在2.3的手机上运行是没有问题的,在2.3后的手机上运行就会出现此异常。

在2.3之后的android不允许在主线程中访问网络,因为访问网络是耗时操作,如果在主线程中访问网络,在网络访问慢时,用户界面会假死,造成Android not Response,为了避免这个问题,2.3之后做了限制,必须将耗时操作放在子线程中完成!!!

android网络访问异常:android.os.NetworkOnMainThreadException

标签:android   style   http   io   ar   color   os   sp   java   

原文地址:http://blog.csdn.net/liang5630/article/details/41910909

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!