致命例外:在改造网络请求期间:Kotlin Coroutine

时间:2019-11-07 05:17:46

标签: 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)

试图增加超时时间,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()

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

1 个答案:

答案 0 :(得分:0)

根据导致应用程序崩溃的错误-在调用逻辑中,您尝试捕获错误的内容:)它的格式应如下:

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)

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

或者您可以尝试CoroutineExceptionHandler

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

//when you make request:

scope.launch(Dispatchers.IO + exceptionHandler ){

}