java拦截器在我关闭internet时抛出ioexception:unknownhostexception

shstlldc  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(379)

使用新版本的okhttp(4.9.0,不确定是否在较低版本)和改进的2.9.0(也是较低版本)。我开始在firebase和google分析报告中看到未知的异常。我试着重述这次崩溃,发现它发生时,互联网是关闭或非常低的质量。我在框架层调用addinterceptor,看到它正在崩溃。我不想在我的崩溃分析中看到这个崩溃。
在“override fun intercept”中有没有处理这个错误的方法(我可以在它进入真正的funs之前在那里捕捉到这个错误)?
以下是firebase的日志之一:

at libcore.io.Linux.android_getaddrinfo(Linux.java)
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)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at okhttp3.Dns$Companion$DnsSystem.lookup(Dns.kt:49)
at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:164)
at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:129)
at okhttp3.internal.connection.RouteSelector.next(RouteSelector.kt:71)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:205)
at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.slipstream.accuradio.framework.intercertors.ApiInterceptor.intercept(ApiInterceptor.kt:23)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919).

谢谢您

ikfrs5lh

ikfrs5lh1#

您应该在普通应用程序代码的拦截器之外捕获和处理此异常。对于正确的解决方案,您还可以查看缓存和提供过时结果是否有帮助。
但在拦截器里你只有三个选择,
抛出ioexception,或者
在应用程序拦截器(不是网络拦截器)的情况下,您可以以某种方式重定向它。这只有在工作网络上有备用服务器时才有帮助,所以这可能没有帮助。
返回一个您自己构建的假响应,但这似乎很奇怪,而不是仅仅处理调用代码中的io异常。

相关问题