【问题标题】:FATAL EXCEPTION: During Retrofit network request : Kotlin Coroutine致命异常:在改造网络请求期间:Kotlin Coroutine
【发布时间】:2019-11-07 19:23:17
【问题描述】:

我在 android kotlin 中使用协程和改造进行 API 调用时遇到致命异常。应用程序崩溃并出现以下异常。

E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-7
Process: com.jamhub.barbeque, PID: 18525
java.net.SocketTimeoutException: timeout
    at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.java:656)
    at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:664)
    at okhttp3.internal.http2.Http2Stream.takeHeaders(Http2Stream.java:153)
    at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:131)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

尝试添加超时,CoroutineExceptionHandler

val client = OkHttpClient().newBuilder().addInterceptor(object : Interceptor {
                    override fun intercept(chain: Interceptor.Chain): Response {
                        val request = chain.request()
                            .newBuilder()
                            .build()
                        return chain.proceed(request)
                    }
                }).callTimeout(30, TimeUnit.SECONDS)
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .build()

                retrofitSocial = Retrofit.Builder()
                    .baseUrl(BuildConfig.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build()

如果异常发生理想情况下它不应该崩溃它应该返回请求失败。

【问题讨论】:

  • 尝试将您的connectTimeout 增加到 30 或更多。
  • 我必须处理异常以避免崩溃。所以增加时间可能无助于避免崩溃。
  • 你的改造电话是什么样的?你想抓住它吗?
  • @Egor 我有。 val api = InjectorUtils.retrofitInstance?.create(LoginAPI::class.java) CoroutineScope(Dispatchers.IO).launch { val response = api?.generateOTP(otpRequestBody) withContext(Dispatchers.Main) { try { when (response?.代码()) { } } 捕捉(e:HttpException){ } } }
  • This 可能会有所帮助

标签: android kotlin kotlin-coroutines


【解决方案1】:

根据导致应用程序崩溃的错误 - 在您的调用逻辑中,您正在尝试捕捉错误的东西 :) 它应该是这样形成的:

try {
    val response = api?.generateOTP(otpRequestBody)
    withContext(Dispatchers.Main) { 
         when (response?.code()) { } } 
catch (e: IOException) { 
}  /* there are many exceptions thrown by Retrofit, all are subclasses of IOException */

因为抛出异常的不是response?.code(),而是api?.generateOTP(otpRequestBody)

至于超时本身 - 您可能有错误的 URL,互联网连接较弱,您需要提供更多信息以便我们找出原因:)

或者你可以试试CoroutineExceptionHandler:

val exceptionHandler = CoroutineExceptionHandler{_ , throwable-> 
 throwable.printStackTrace()
}

//when you make request:

scope.launch(Dispatchers.IO + exceptionHandler ){

}

【讨论】:

  • 我正在尝试使用弱互联网连接,在这种情况下我需要处理应用程序崩溃,这是我的要求。如果可能的话请让我知道。谢谢
  • 是的,捕获 API 调用抛出的异常或使用 CoroutineExceptionHandler 将是可行的方法。
  • 异常没有被缓存,出现以下错误 AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-3 Process: com.jamhub.barbeque, PID: 5462
  • 大家好,你们能帮我吗stackoverflow.com/q/58799264/10800406
猜你喜欢
  • 2019-10-30
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多