gpt4 book ai didi

android - 致命异常 : During Retrofit network request : Kotlin Coroutine

转载 作者:行者123 更新时间:2023-12-02 13:34:52 24 4
gpt4 key购买 nike

我在 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()

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

最佳答案

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

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 ){

}

关于android - 致命异常 : During Retrofit network request : Kotlin Coroutine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58742361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com