java.net.UnknownHostException: 无法解析主机“<url>”: 没有与主机名关联的地址
java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname
我 100% 确定 phone 有互联网连接。我可以浏览网站并且连接有效。尽管如此,在我的应用程序中,我看到了这个错误:
java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname
在源代码中,我提供的URL可以从网络浏览器运行,我可以访问它。
java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname and End of input at character 0 of 的解决方案建议仔细检查 WiFi 或断开连接,然后重新连接到 WiFi 网络。我尝试这样做,甚至重新启动 phone。我肯定可以上网。
有趣的是,一切都适用于 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 时,我在网络浏览器中看到了这个 JSON 响应:
{"result":"Success","Cities":[{"name":"Panama","slug":"panama-city"},{"name":"Colombia","slug":"Bogota"}]}
URL 有效,我有 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:
如果从 phone 和 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”作为 URL,我会在 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”,如您在上面的更新 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.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.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 的可用性无关?
我想我现在需要关闭这个问题,因为它已经与另一个问题直接相关:。因此,对其他问题的解决也将成为对这个问题的解决。
我 100% 确定 phone 有互联网连接。我可以浏览网站并且连接有效。尽管如此,在我的应用程序中,我看到了这个错误:
java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname
在源代码中,我提供的URL可以从网络浏览器运行,我可以访问它。
java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname and End of input at character 0 of 的解决方案建议仔细检查 WiFi 或断开连接,然后重新连接到 WiFi 网络。我尝试这样做,甚至重新启动 phone。我肯定可以上网。
有趣的是,一切都适用于 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 时,我在网络浏览器中看到了这个 JSON 响应:
{"result":"Success","Cities":[{"name":"Panama","slug":"panama-city"},{"name":"Colombia","slug":"Bogota"}]}
URL 有效,我有 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:
如果从 phone 和 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”作为 URL,我会在 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”,如您在上面的更新 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.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.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 的可用性无关?
我想我现在需要关闭这个问题,因为它已经与另一个问题直接相关: