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

5
我遇到了一个问题,我的应用程序使用的是okhttp版本3.12.1 (https://github.com/square/okhttp),即使我已经在AndroidManifest.xml中提供了Internet权限,它也无法连接到域名。但是根据用户的评论,他们可以连接到设备上的互联网并使用其他应用程序。
目前,我无法重现这个异常,如果有人之前遇到过这个问题,希望能帮助我更好地理解。
由于我不想引入大的改变,因此我暂时不能升级库。
堆栈跟踪:
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

在Windows上:ping domain.com -6 会产生相同的错误。不确定您的用户是否需要IPv6地址... - greeble31
你找到解决方案了吗?如果是的话,请分享一下。我也遇到了同样的问题。 - Nirmal Shethwala
有时候我会遇到这样的问题,如果我的域名是'www.example.com',我通常会在'www.example.com'前面加上'http://'。 - tendai
这种情况发生在我的模拟器无法连接到互联网时。我尝试了各种方法,最终创建了一个使用SDK版本34的不同模拟器。 - undefined
6个回答

0

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

  • 减少了一些 CNAME 记录。
  • 降低了 TTL。

在 Android 客户端没有进行任何代码更改。


0

可能是获取API的设备没有连接互联网。请连接互联网并重试!


2
受此问题影响的用户表示他们有互联网连接,因为他们可以使用其他应用程序。 - Eric Aya

0

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

希望这能帮到你,兄弟。


2
受此问题影响的用户表示,他们具有互联网连接,因为他们能够使用其他应用程序。服务器端点也正常运行且没有错误,因此iOS用户不受影响。AndroidManifest未链接任何网络安全配置规则。 - scout
我遇到了类似的问题,你找到任何解决方案了吗? - Kartik Garasia

0
根据用户的ISP可能会发生这种情况,我认为确保域名得到解析的最佳方法是使用自定义DNS解析器,该解析器基于需求使用自定义缓存机制。
有时候ISP/路由器会根据第一次查询缓存DNS响应,
所以大多数情况下,你从主要DNS服务器请求记录都能正常工作。 请查看https://square.github.io/okhttp/4.x/okhttp/okhttp3/-dns/

0
虽然这是一个老问题。 参考this link 添加这个依赖项
    implementation 'com.github.dnsjava:dnsjava:3.2.2'

Dns类
class CustomDns : Dns {

private fun lookupFrom(hostname: String): List<InetAddress> {
    val simpleResolver = SimpleResolver(GOOGLE_DNS);
    val addresses = mutableListOf<InetAddress>()

    val lookup = Lookup(hostname)
    lookup.setResolver(simpleResolver)
    val arrayOfRecords = lookup.run()
    if (arrayOfRecords != null)
        for (record in arrayOfRecords)
            if (record.type == Type.A)
                addresses += ((record as ARecord).address)

    val lookup1 = Lookup(hostname, Type.AAAA)
    lookup1.setResolver(simpleResolver)
    val arrayOfRecords1 = lookup1.run()
    if (arrayOfRecords1 != null)
        for (record in arrayOfRecords1)
            if (record.type == Type.AAAA)
                addresses += ((record as AAAARecord).address)

    if (addresses.size < 1)
        throw UnknownHostException("Host '$hostname' unknown to '$GOOGLE_DNS'")
    return addresses
}

override fun lookup(hostname: String): List<InetAddress> {
    return try {
        try {
            InetAddress.getAllByName(hostname).toList()  // 1. try: original to system library
        } catch (e: UnknownHostException) {
            try {
                Address.getAllByName(hostname).toList()  // 2. try: DNSJava default lookup
            } catch (e: UnknownHostException) {
                lookupFrom(hostname)  // fallback: tailored lookup via DNSJava
            }
        }
    } catch (e: NullPointerException) {
        throw UnknownHostException("Broken system behaviour for dns lookup of $hostname").apply {
            initCause(e)
        }
    }
}

companion object{
    private const val GOOGLE_DNS = "8.8.8.8"
}}

我使用了Hilt来为这个DNS提供OKHttp Client。你可以根据你的项目设置进行配置。
object CustomOkHttpClient {

private val customDns = CustomDns()

val instance: OkHttpClient = OkHttpClient.Builder()
    .dns(customDns)
    // Add any other OkHttpClient customization if needed
    .build()}

-2
我在使用Android中的Volley时遇到了类似的问题。在清除缓存并重新启动项目后,问题得到了解决。
在Android Studio中, 文件 -> 清除缓存 -> 选择所有复选框 -> 清除缓存并重启。

问题与用户的设备有关,而不是与Android Studio本身有关。 - Koorosh Ghorbani
问题与用户的设备有关,而不是与Android Studio本身有关。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接