android java.net.UnknownHostException: Unable to resolve host "...": No address associated 错误

时间:2020-12-23 15:54:07

转载地址:http://blog.csdn.net/murongshusheng/article/details/7623188

报错log:

03-07 15:34:38.645: W/System.err(8731): java.net.UnknownHostException: Unable to resolve host "images.liqucn.com": No address associated with hostname
03-07 15:34:38.655: W/System.err(8731):  at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
03-07 15:34:38.655: W/System.err(8731):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
03-07 15:34:38.655: W/System.err(8731):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
03-07 15:34:38.655: W/System.err(8731):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
03-07 15:34:38.655: W/System.err(8731):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-07 15:34:38.655: W/System.err(8731):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
03-07 15:34:38.655: W/System.err(8731):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
03-07 15:34:38.655: W/System.err(8731):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-07 15:34:38.665: W/System.err(8731):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-07 15:34:38.665: W/System.err(8731):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
03-07 15:34:38.665: W/System.err(8731):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-07 15:34:38.665: W/System.err(8731):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-07 15:34:38.665: W/System.err(8731):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
03-07 15:34:38.665: W/System.err(8731):  at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
03-07 15:34:38.665: W/System.err(8731):  at com.example.duandianxuchuan.MainActivity$2.run(MainActivity.java:78)
03-07 15:34:38.665: W/System.err(8731):  at java.lang.Thread.run(Thread.java:856)
03-07 15:34:38.680: W/System.err(8731): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
03-07 15:34:38.685: W/System.err(8731):  at libcore.io.Posix.getaddrinfo(Native Method)
03-07 15:34:38.685: W/System.err(8731):  at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
03-07 15:34:38.685: W/System.err(8731):  at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
03-07 15:34:38.685: W/System.err(8731):  ... 15 more

代码是:conn.getResponseCode()

在android开发的时候经常会遇到这个错误,一般来说,造成这种错误的最普遍情况有两种:

1.android设备网络连接没打开,例如3G网络和WIFI网络

     所以,如果遇到这种错误时,请先查看网络是否已正常连接.

 2.Manifest文件没有标明网络访问权限

     如果确认网络已经正常连接并且还是出这种错误的话,那么请看下你的Manifest文件是否标明应用需要网络访问权限,如果没标明的话,也访问不了网络,也会造成这种情况的.

     //网络访问权限

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