java.net.UnknownHost异常:无法解析主机"domain.com":没有与主机名关联的地址



遇到了一个问题,即我的应用程序使用 https://github.com/square/okhttp - okhttp 版本 - 3.12.1 无法连接到域,即使我在 Android清单中提供了互联网权限.xml

但是,根据用户的评论,用户可以在其设备上连接到互联网并使用其他应用程序。

目前,我无法重现该异常,因此,如果以前遇到过此问题的任何人可以帮助我更好地理解。

我目前无法升级库,因为我不想引入大的更改。

堆栈跟踪

Caused by: java.net.UnknownHostException: Unable to resolve host "domain.com": No address associated with hostname
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:156)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at okhttp3.Dns$-CC.lambda$static$0(SourceFile:39)
at okhttp3.-$$Lambda$Dns$mTkNcZf2K4euny3_jks6Cac6Az0.lookup(Unknown Source:0)
at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(SourceFile:185)
at okhttp3.internal.connection.RouteSelector.nextProxy(SourceFile:149)
at okhttp3.internal.connection.RouteSelector.next(SourceFile:84)
at okhttp3.internal.connection.StreamAllocation.findConnection(SourceFile:214)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(SourceFile:135)
at okhttp3.internal.connection.StreamAllocation.newStream(SourceFile:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(SourceFile:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(SourceFile:94)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(SourceFile:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(SourceFile:125)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
at com.appdynamics.eumagent.runtime.a.a$a.intercept(SourceFile:108)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(SourceFile:264)
at okhttp3.RealCall.execute(SourceFile:93)
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Linux.android_getaddrinfo(Native Method)
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)
at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:200)
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)
... 39 more

我通常会在使用 Retrofit(使用 Okhttp 作为客户端(时遇到该错误,当设备没有任何互联网连接(WIFI 或移动数据(或我的服务器端点关闭时就会发生这种情况。自己试试吧!但还有其他几个因素;如运行时请求权限或网络安全配置。

希望这有帮助,兄弟。

我们通过进行一些 DNS 更改解决了此问题。

  • 减少了很少的 CNAME 记录。
  • 降低 TTL。

安卓客户端上没有代码更改。

根据用户的ISP可能会发生,我认为保证域解析的最佳方法是使用自定义DNS解析器,根据需要使用自定义缓存机制.
有时ISP/路由器会根据第一个查询缓存DNS响应,
所以大多数时候你从它工作的主要DNS服务器请求记录。 看看 https://square.github.io/okhttp/4.x/okhttp/okhttp3/-dns/

我在安卓中使用 Volley 时遇到了类似的问题。使缓存失效并重新启动项目后,该问题已得到解决。

在 Android Studio 中, 文件 -> 使缓存失效 -> 选中所有复选框 -> 使缓存失效并重新启动。

相关内容

最新更新