java.net.UnknownHostException:无法解析主机“ <URL>”:没有与主机名关联的地址

时间:2018-07-02 00:10:40

标签: android android-wifi android-4.2-jelly-bean

我100%确定手机已连接互联网。我可以浏览网站,并且连接正常。但是,从我的应用程序中,我看到此错误:

java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname

在源代码中,我提供的URL可通过Web浏览器使用,我可以访问它。

java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname and End of input at character 0 of处的解决方案建议仔细检查WiFi或断开连接,然后重新连接到WiFi网络。我尝试这样做,甚至重新启动手机。我肯定可以上网。

有趣的是,一切都可以在Android 6.x及更高版本上运行。仅在Android 4.x和5.x中,此错误才发生在我的应用程序中。

更新1:

我试图从应用程序访问的URL的代码:

public static final String MY_URL = CORE_URL + "Panama/products/json_index/";

当我访问存储在MY_URL中的URL时,在Web浏览器中看到了JSON响应:

{"result":"Success","Cities":[{"name":"Panama","slug":"panama-city"},{"name":"Colombia","slug":"Bogota"}]}

该网址有效,并且我有WiFi。一切都适用于Android 6.x及更高版本。奇怪只有Android 4.x和5.x失败。

更新2:

这是我稍后在代码中引用URL的方式:

Util.requestGetData(Util.MY_URL, this, ResponseListener.REQUEST_MY_CITIES_LIST);

我也使用这个:

@Override
public void onResponse(final Response r, final int rid) {
    ...
    docment = new JSONObject(r.getData());
    ...
}

当我使用r.getData()时会发生问题。

更新3:

如果从装有Android 4.x的手机上访问该URL,则会得到以下信息:

{"result":"Success","Cities":[{"name":"Panama","slug":"panama-city"},{"name":"Colombia","slug":"Bogota"}]}

URL有效,我可以访问互联网,从我所看到的来看,WiFi很好,因为我可以浏览网站并且一切正常。也许是间歇性WiFi?对我来说,WiFi一切都很好,互联网访问也很好。当我从Android 6.x及更高版本使用r.getData()时,该应用程序可以正常工作。我收到此错误,可能在Android 4.x和5.x中发生了什么。奇怪的是,它以前曾经可以工作,但是在最近的几周里,它突然停止了工作,而我根本没有更改任何代码。

更新4:

如果我在代码中使用“ http://www.google.com”作为网址,那么我在Android Studio Logcat中遇到了另一个错误,但在应用程序中却出现了完全相同的问题:

07-01 19:28:58.791 28351-28399/com.myapp D/tag: http://www.google.com
07-01 19:28:59.171 28351-28351/com.myapp D/OpenGLRenderer: Enabling debug mode 0
07-01 19:28:59.171 28351-28351/com.myapp D/ProgressBar: updateDrawableBounds: left = 0
07-01 19:28:59.171 28351-28351/com.myapp D/ProgressBar: updateDrawableBounds: top = 0
07-01 19:28:59.171 28351-28351/com.myapp D/ProgressBar: updateDrawableBounds: right = 72
07-01 19:28:59.171 28351-28351/com.myapp D/ProgressBar: updateDrawableBounds: bottom = 72
07-01 19:28:59.231 28351-28407/com.myapp I/dalvikvm: Could not find method android.content.Context.checkSelfPermission, referenced from method com.urbanairship.analytics.EventApiClient.getLocationPermission
07-01 19:28:59.231 28351-28407/com.myapp W/dalvikvm: VFY: unable to resolve virtual method 138: Landroid/content/Context;.checkSelfPermission (Ljava/lang/String;)I
07-01 19:28:59.231 28351-28407/com.myapp D/dalvikvm: VFY: replacing opcode 0x6e at 0x000c
07-01 19:28:59.431 28351-28351/com.myapp E/ViewRootImpl: sendUserActionEvent() mView == null
07-01 19:28:59.471 28351-28351/com.myapp W/InputMethodManager: Ignoring onBind: cur seq=57, given seq=56

更新5:

我将MY_URL中的值更改为“ http://www.google.com”,如您在上面的UPDATE 4中所见。现在,我将其更改回以前的URL,现在看到的错误有所不同,但应用程序仍然以相同的方式失败。现在这是错误:

07-01 19:36:31.931 28645-28645/com.myapp D/OpenGLRenderer: Enabling debug mode 0
07-01 19:36:31.931 28645-28645/com.myapp D/ProgressBar: updateDrawableBounds: left = 0
07-01 19:36:31.931 28645-28645/com.myapp D/ProgressBar: updateDrawableBounds: top = 0
07-01 19:36:31.931 28645-28645/com.myapp D/ProgressBar: updateDrawableBounds: right = 72
07-01 19:36:31.931 28645-28645/com.myapp D/ProgressBar: updateDrawableBounds: bottom = 72
07-01 19:36:32.011 28645-28705/com.myapp I/dalvikvm: Could not find method android.content.Context.checkSelfPermission, referenced from method com.urbanairship.analytics.EventApiClient.getLocationPermission
07-01 19:36:32.011 28645-28705/com.myapp W/dalvikvm: VFY: unable to resolve virtual method 138: Landroid/content/Context;.checkSelfPermission (Ljava/lang/String;)I
07-01 19:36:32.011 28645-28705/com.myapp D/dalvikvm: VFY: replacing opcode 0x6e at 0x000c
07-01 19:36:32.031 28645-28698/com.myapp W/System.err: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at com.android.org.conscrypt.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:146)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:388)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-01 19:36:32.031 28645-28698/com.myapp W/System.err:     at com.myapp.Util.Util$1.run(Util.java:242)
07-01 19:36:32.041 28645-28698/com.myapp W/System.err:     at java.lang.Thread.run(Thread.java:841)
07-01 19:36:32.251 28645-28645/com.myapp E/ViewRootImpl: sendUserActionEvent() mView == null
07-01 19:36:32.301 28645-28645/com.myapp W/InputMethodManager: Ignoring onBind: cur seq=60, given seq=59

更新6:

现在我一直看到此错误,并且java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname再也不会出现在Android Studio Logcat中:

07-02 09:30:15.468 9350-9391/com.myapp W/System.err: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at com.android.org.conscrypt.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:146)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:388)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at com.myapp.Util.Util$1.run(Util.java:242)
07-02 09:30:15.478 9350-9391/com.myapp W/System.err:     at java.lang.Thread.run(Thread.java:841)
07-02 09:30:15.628 9350-9350/com.myapp E/ViewRootImpl: sendUserActionEvent() mView == null
07-02 09:30:15.668 9350-9350/com.myapp W/InputMethodManager: Ignoring onBind: cur seq=77, given seq=76

也许真正的问题是SSL证书,它与WiFi问题或我正在使用的URL的可用性无关?

1 个答案:

答案 0 :(得分:0)

我想我现在需要结束这个问题,因为它已经与以下另一个问题直接相关:javax.net.ssl.SSLPeerUnverifiedException: No peer certificate on Android 4.x and 5.x。因此,其他问题的解决方案也将成为该问题的解决方案。