安卓:Retrofit2和Kotlin无法捕捉的例外

人气:485 发布:2022-10-16 标签: android kotlin retrofit2

问题描述

我遇到了一个关于最新的Retrofit2和Kotlin的非常困难的问题。

客户端希望通过改装2连接到服务器。

        val client = OkHttpClient.Builder()
            .addInterceptor(object: Interceptor {
                override fun intercept(chain: Interceptor.Chain): Response {
                    try{
                        chain.proceed(chain.request());
                        return chain.proceed(chain.request());
                     }catch(e:Exception){
                        e.printStackTrace()
                      //called
                     }

                    return chain.proceed(chain.request());//leads to crash
                }
            })
            .readTimeout(10, TimeUnit.SECONDS).build()

升级服务的创建方式如下:

        try {//try/catch here is useless -> not called
        retrofit = Retrofit.Builder()
                .baseUrl(Constants.SERVER_BASEURL + ":3000")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }catch (e:Exception){
            e.printStackTrace()
        }

任何函数都以协程方式调用,如下所示:

@POST("/users/me/logout")
suspend fun logout(@Header("Authorization") token: String): ResponseBody

所有这些都运行良好,但一旦断开与服务器的连接,就无法捕获错误,这会自动导致应用程序崩溃。

对于脱机服务器,异常如下所示:

java.net.ConnectException: Failed to connect to /XXXXXXX:3000
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:270)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:176)
    at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:236)
    at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:109)
    at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:77)
    at okhttp3.internal.connection.Transmitter.newExchange$okhttp(Transmitter.kt:162)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:35)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
    at com.project.app.Helpers.MasterViewModel$getRetrofitClient$client$1.intercept(MasterViewModel.kt:118)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
    at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:215)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
    at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
    at java.util.concurrent.ThreadPoolExecutor.processTask(ThreadPoolExecutor.java:1187)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:784)

我所知道的:

截取第一个代码块中的事件表明异常是在

chain.Continue(chain.request());

因为"intercept"的返回值是"Response",所以可以认为只返回一个伪响应。但这也导致了新的问题,因为创建这样一个虚拟响应非常困难,而且您还会丢失所有请求调用。最好是在服务器重新连接之前只挂起发出的请求。

第二个代码块内的Try,Catch块实际上毫无用处。它无法防止崩溃,因为崩溃发生在请求/响应管道中,可能发生在通过改进以调用挂起函数而启动的协程中。

服务器超时或根本不存在都不要紧,只要出现连接问题,就会抛出无法捕获的异常,应用就会崩溃

我想,这是一个尚未解决的一般性问题。

还有类似的问题,比如这个:

Retrofit and OkHttpClient, catch connection timeout in failure method

但是解决方案在这里不起作用,因为它不会阻止调用关键部分"chain.Progress(chain.request());"

除分叉建议外,任何帮助都将不胜感激。

推荐答案

我想您不需要try/catch升级初始化(也不需要自定义OkhttpClient),而是需要每次调用服务。创建apiClient并重用以调用服务:

val apiClient = retroft.create(TourService::class.java)
...
//calling API:
try{
    val responseBody = apiClient.logout("some token")
}catch(x: Exception){
    //catch app crash
    when(x){
        is ConnectException -> //TODO: handle connection error       
        is UnknownHostException ->  //TODO: handle no internet connection error
    }
}

625