android.system.GaiException

2020-02-15  本文已影响0人  好学人

错误日志

完整的错误日志如下:

1 java.net.UnknownHostException:Unable to resolve host "xxx.xxx.com": No address associated with hostname
2 java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:157)
3 ......
4 Caused by:
5 android.system.GaiException:android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
6 libcore.io.Linux.android_getaddrinfo(Native Method)
7 libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:172)
8 java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:137)
9 java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
10 java.net.InetAddress.getAllByName(InetAddress.java:1154)
11 okhttp3.Dns$1.lookup(Dns.java:40)
12 okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
13 okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
14 okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
15 okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:215)
16 okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
17 okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
18 okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
19 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
20 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
21 okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
22 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
23 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
24 okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
25 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
26 okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
27 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
28 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
29 okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
30 okhttp3.RealCall$AsyncCall.execute(RealCall.java:201)
31 okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
32 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
33 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
34 java.lang.Thread.run(Thread.java:764)

参考资料

Handling Network Error in Rxjava 2 - Retrofit 2

android/system/GaiException.java

Android UnkownHostExceptin 解析

解决方案

方案1:

捕捉RxJava全局异常

RxJavaPlugins.setErrorHandler(Throwable::printStackTrace); 

方案2:

This is because RxJava2Adapter creates a call that is not deferred at all, it is executed immediately upon subscription, so it goes to onError at the very first step and you'll receive UnknownHostException immediately on subscribing.

I wanted to add a valve to ensure RxJava singles run only if there is network availability, for this I had to clone out RxJava2Adapter and add some hacky code:

详情参考:https://github.com/ReactiveX/RxJava/issues/4942#issuecomment-411288366

上一篇下一篇

猜你喜欢

热点阅读