java - Android:无法处理 UnknownHostException [使用 Retrofit 2、okhttp3、rxandroid 2]

标签 java android rx-android okhttp

我有一个 Android 应用程序,它已经投入生产一段时间了。我收到了一份发给 Firebase 的致命报告(日志如下),但我不清楚下次如何避免这种情况。

我在这里找到了类似的堆栈跟踪java.net.UnknownHostException Unable to resolve host "accounts.google.com": No address associated with hostname while inserting rows in bigquery但他们能够解决这个问题:

<uses-permission android:name="android.permission.INTERNET" />

但这不是我的情况。

对于网络通信,我使用:

  • 改造2
  • 确定http 3
  • 用于 Retrofit 2 的 rxjava2 适配器

使用此设置,所有错误都会在 rx 订阅的 onError 函数中结束。我在所有 HTTP 调用中处理 UnknownHostException,但即使我没有处理,它也应该将非致命报告为最坏的情况。

我不知道如何重现这个错误。堆栈跟踪不是很有帮助,因为它没有将我指向我的应用程序的任何位置。

任何有关如何重现该错误的想法以及任何其他见解都将非常感激。

这是堆栈跟踪:

Exception java.net.UnknownHostException: Unable to resolve host "rest.mywebsite.com": No address associated with hostname
java.net.InetAddress.lookupHostByName (InetAddress.java:470)
java.net.InetAddress.getAllByNameImpl (InetAddress.java:252)
java.net.InetAddress.getAllByName (InetAddress.java:215)
okhttp3.Dns$1.lookup (Dns.java)
okhttp3.internal.connection.RouteSelector.hasNext (RouteSelector.java)
<OR>.connectFailed (RouteSelector.java)
<OR>.resetNextProxy (RouteSelector.java)
<OR>.resetNextInetSocketAddress (RouteSelector.java)
<OR>.getHostString (RouteSelector.java)
okhttp3.internal.connection.RouteSelector.nextProxy (RouteSelector.java)
okhttp3.internal.connection.RouteSelector.next (RouteSelector.java)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java)
<OR>.findHealthyConnection (StreamAllocation.java)
<OR>.findConnection (StreamAllocation.java)
<OR>.streamFinished (StreamAllocation.java)
<OR>.codec (StreamAllocation.java)
<OR>.deallocate (StreamAllocation.java)
<OR>.streamFailed (StreamAllocation.java)
<OR>.acquire (StreamAllocation.java)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java)
<OR>.findHealthyConnection (StreamAllocation.java)
<OR>.findConnection (StreamAllocation.java)
<OR>.streamFinished (StreamAllocation.java)
<OR>.codec (StreamAllocation.java)
<OR>.deallocate (StreamAllocation.java)
<OR>.streamFailed (StreamAllocation.java)
<OR>.acquire (StreamAllocation.java)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java)
<OR>.findHealthyConnection (StreamAllocation.java)
<OR>.findConnection (StreamAllocation.java)
<OR>.streamFinished (StreamAllocation.java)
<OR>.codec (StreamAllocation.java)
<OR>.deallocate (StreamAllocation.java)
<OR>.streamFailed (StreamAllocation.java)
<OR>.acquire (StreamAllocation.java)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java)
<OR>.stripBody (CacheInterceptor.java)
<OR>.maybeCache (CacheInterceptor.java)
<OR>.cacheWritingResponse (CacheInterceptor.java)
<OR>.combine (CacheInterceptor.java)
<OR>.isEndToEnd (CacheInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java)
<OR>.cookieHeader (BridgeInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RetryAndFollowUpInterceptor.cancel (RetryAndFollowUpInterceptor.java)
<OR>.setCallStackTrace (RetryAndFollowUpInterceptor.java)
<OR>.intercept (RetryAndFollowUpInterceptor.java)
<OR>.createAddress (RetryAndFollowUpInterceptor.java)
<OR>.recover (RetryAndFollowUpInterceptor.java)
<OR>.isRecoverable (RetryAndFollowUpInterceptor.java)
<OR>.followUpRequest (RetryAndFollowUpInterceptor.java)
<OR>.sameConnection (RetryAndFollowUpInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.logging.HttpLoggingInterceptor.setLevel (HttpLoggingInterceptor.java)
<OR>.intercept (HttpLoggingInterceptor.java)
<OR>.isPlaintext (HttpLoggingInterceptor.java)
<OR>.bodyEncoded (HttpLoggingInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.RealCall.getResponseWithInterceptorChain (RealCall.java)
okhttp3.RealCall$AsyncCall.execute (RealCall.java)
okhttp3.internal.NamedRunnable.run (NamedRunnable.java)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1113)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:588)
java.lang.Thread.run (Thread.java:818)

Caused by android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
libcore.io.Posix.android_getaddrinfo (Posix.java)
libcore.io.ForwardingOs.android_getaddrinfo (ForwardingOs.java:55)
java.net.InetAddress.lookupHostByName (InetAddress.java:451)
java.net.InetAddress.getAllByNameImpl (InetAddress.java:252)
java.net.InetAddress.getAllByName (InetAddress.java:215)
okhttp3.Dns$1.lookup (Dns.java)
okhttp3.internal.connection.RouteSelector.hasNext (RouteSelector.java)
<OR>.connectFailed (RouteSelector.java)
<OR>.resetNextProxy (RouteSelector.java)
<OR>.resetNextInetSocketAddress (RouteSelector.java)
<OR>.getHostString (RouteSelector.java)
okhttp3.internal.connection.RouteSelector.nextProxy (RouteSelector.java)
okhttp3.internal.connection.RouteSelector.next (RouteSelector.java)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java)
<OR>.findHealthyConnection (StreamAllocation.java)
<OR>.findConnection (StreamAllocation.java)
<OR>.streamFinished (StreamAllocation.java)
<OR>.codec (StreamAllocation.java)
<OR>.deallocate (StreamAllocation.java)
<OR>.streamFailed (StreamAllocation.java)
<OR>.acquire (StreamAllocation.java)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java)
<OR>.findHealthyConnection (StreamAllocation.java)
<OR>.findConnection (StreamAllocation.java)
<OR>.streamFinished (StreamAllocation.java)
<OR>.codec (StreamAllocation.java)
<OR>.deallocate (StreamAllocation.java)
<OR>.streamFailed (StreamAllocation.java)
<OR>.acquire (StreamAllocation.java)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java)
<OR>.findHealthyConnection (StreamAllocation.java)
<OR>.findConnection (StreamAllocation.java)
<OR>.streamFinished (StreamAllocation.java)
<OR>.codec (StreamAllocation.java)
<OR>.deallocate (StreamAllocation.java)
<OR>.streamFailed (StreamAllocation.java)
<OR>.acquire (StreamAllocation.java)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java)
<OR>.stripBody (CacheInterceptor.java)
<OR>.maybeCache (CacheInterceptor.java)
<OR>.cacheWritingResponse (CacheInterceptor.java)
<OR>.combine (CacheInterceptor.java)
<OR>.isEndToEnd (CacheInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java)
<OR>.cookieHeader (BridgeInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RetryAndFollowUpInterceptor.cancel (RetryAndFollowUpInterceptor.java)
<OR>.setCallStackTrace (RetryAndFollowUpInterceptor.java)
<OR>.intercept (RetryAndFollowUpInterceptor.java)
<OR>.createAddress (RetryAndFollowUpInterceptor.java)
<OR>.recover (RetryAndFollowUpInterceptor.java)
<OR>.isRecoverable (RetryAndFollowUpInterceptor.java)
<OR>.followUpRequest (RetryAndFollowUpInterceptor.java)
<OR>.sameConnection (RetryAndFollowUpInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.logging.HttpLoggingInterceptor.setLevel (HttpLoggingInterceptor.java)
<OR>.intercept (HttpLoggingInterceptor.java)
<OR>.isPlaintext (HttpLoggingInterceptor.java)
<OR>.bodyEncoded (HttpLoggingInterceptor.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.internal.http.RealInterceptorChain.request (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.proceed (RealInterceptorChain.java)
<OR>.sameConnection (RealInterceptorChain.java)
okhttp3.RealCall.getResponseWithInterceptorChain (RealCall.java)
okhttp3.RealCall$AsyncCall.execute (RealCall.java)
okhttp3.internal.NamedRunnable.run (NamedRunnable.java)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1113)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:588)
java.lang.Thread.run (Thread.java:818)

手机网络断开,发生在 Android 6.0.1 上。

最佳答案

该问题导致嵌套 Rx 调用。当我取消订阅一个 Rx 调用时,嵌套的调用仍然存在并导致崩溃。

关于java - Android:无法处理 UnknownHostException [使用 Retrofit 2、okhttp3、rxandroid 2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43135211/

相关文章:

java - 使用其他 Observable 过滤 RxJava Observable

android - 如果适配器的数据通过 RxAndroid 设置,则 RecyclerView 在返回导航后滚动到顶部

android - 将累积的请求发送到 Web 服务 - RxJava2

android - 在 Android 中为 Navigation Drawer 添加一节分隔符

android - JsonSubTypes,多态对象列表和 Parcelable

android - 从 Eclipse 安装时如何解锁 Android 屏幕

java - 为 Spring Controller 自动生成客户端

Java,使方法静态

java - Thymeleaf:将 webjar CSS 文件的内容插入样式标签

java - 将 XML 转换为 JSON,与 FasterXML 一起使用的 jaxb 类出现问题