当前位置: 代码迷 >> Android >> [原]Android 初遇Http异常 httpClient.execute
  详细解决方案

[原]Android 初遇Http异常 httpClient.execute

热度:528   发布时间:2016-05-01 09:56:49.0
[原]Android 初遇Http错误 httpClient.execute

错误源头:

HttpResponse response = httpClient.execute(httpget);

错误信息:

android.os.NetworkOnMainThreadExceptionat android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)at java.net.InetAddress.lookupHostByName(InetAddress.java:385)at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)at java.net.InetAddress.getAllByName(InetAddress.java:214)at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

解决办法:

1. 4.0以上就不允许在UI线程里面进行网络,是涉及到url操作的时候,访问网络不能在主程序中运行

2. <uses-permission android:name="android.permission.INTERNET" />

参考:

首次测试android程序连接网络错误:http://blog.csdn.net/dragon08/article/details/7666172

?

?

  相关解决方案