美文网首页
Android OkHttp IOException unexp

Android OkHttp IOException unexp

作者: YoriZ | 来源:发表于2019-12-19 14:21 被阅读0次

    先说处理方式,http请求时添加以下header

    header ("Connection":"close")
    

    如果是使用Retrofit基类添加Header需要使用拦截器,代码如下:

    val client = OkHttpClient.Builder()
                    .addInterceptor {
                        val original = it.request()
                        val request = original.newBuilder()
                                .header("Connection":"close")
                                .method(original.method(), original.body())
                                .build()
                        it.proceed(request)
                    }
                    .build()
    val retrofit = Retrofit.Builder().apply {
                baseUrl(baseUrl)
                client(client)
                addConverterFactory(GsonConverterFactory.create())
                addCallAdapterFactory(CoroutineCallAdapterFactory())
            }
    

    处理方式来自OkHttp3 - IOException: unexpected end of stream on

    issues中没说是为什么导致,但是header中是"Connection":"close",查阅了代码后推测是否因为客户端与服务端timeout不同或其他原因导致一方关闭连接后另一方认为还处在连接状态后导致,添加connection-close后明确处理完毕被close掉。

    经查阅Connection-close解释

    HTTP Connection的 close设置允许客户端或服务器中任何一方关闭底层的连接双方都会要求在处理请求后关闭它们的TCP连接。

    看到这里之后决定同时在服务端的response中添加header ("Connection":"close")

    在此之后暂未出现此异常

    相关文章

      网友评论

          本文标题:Android OkHttp IOException unexp

          本文链接:https://www.haomeiwen.com/subject/mmpmnctx.html