android - 好的http。异常 java.io.IOException : unexpected end of stream on Connection

标签 android okhttp ioexception

我遇到了一个问题,大约是 OkHttpClient 的三分之一newCall(request).execute();对服务器的请求失败,但出现以下异常:

Exception java.io.IOException: unexpected end of stream on Connection{[REDACTED_DOMAIN_NAME]:80, proxy=DIRECT@ hostAddress=[REDACTED_DOMAIN_NAME]/[REDACTED_IP_ADDRESS] cipherSuite=none protocol=http/1.1}

我禁用了证书验证。这种行为的原因可能是什么?

最佳答案

OkHttpClient 的默认请求协议(protocol)是 HTTP2.0。
某些 Web 服务需要 HTTP1.1 请求。
将protocols(listOf(Protocol.HTTP_1_1)) 行添加到您的OkHttpClient。
例子;

val okHttpClient = OkHttpClient.Builder()
                    .protocols(listOf(Protocol.HTTP_1_1))
                    .readTimeout(40, TimeUnit.SECONDS)
                    .connectTimeout(40, TimeUnit.SECONDS)
                    .addInterceptor(loggingInterceptor)
                    .addInterceptor(authInterceptor)
                    .retryOnConnectionFailure(true)
                    .build()

关于android - 好的http。异常 java.io.IOException : unexpected end of stream on Connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49433188/

相关文章:

android - VLC 实时 http 流式传输到 Android

android - Android 中的错误 -- 无法找到 : ComponentInfo 的检测信息

java.io.IOException : Invalid Http response 异常

java - 正确放置尝试和捕获

android - onNewIntent 没有被调用

java - 如何使用使用json对象获取数据并显示在listview中?

android - 如何在 Interceptor Android 中获取 401 状态代码时更改 Activity

Android Retrofit 2 文件上传不起作用

android - 无法解析方法 setClient(okhttp3.OkHttpClient)

java - 了解 Java IOException