致命异常:在 Retrofit 网络请求期间:Kotlin Coroutine

Mat*_*hew 2 android kotlin kotlin-coroutines

我在 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)
Run Code Online (Sandbox Code Playgroud)

尝试添加超时,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()
Run Code Online (Sandbox Code Playgroud)

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

小智 6

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

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 */
Run Code Online (Sandbox Code Playgroud)

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

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

或者您可以尝试CoroutineExceptionHandler

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

//when you make request:

scope.launch(Dispatchers.IO + exceptionHandler ){

}
Run Code Online (Sandbox Code Playgroud)