gpt4 book ai didi

android - HTTP FAILED : java. io.IOException: 发出 https 请求时流异常的意外结束

转载 作者:行者123 更新时间:2023-12-02 02:45:26 25 4
gpt4 key购买 nike

我们以前使用的是 http apis,现在我们已经迁移到 https,使用相同的代码我们面临异常 HTTP FAILED: java.io.IOException: unexpected end of stream(这是针对某些设备和某些网络调用) .我们正在使用 来自 Android 应用程序的 OkHttp 和 Retrofit。下面是我们的代码

@Provides
@ApplicationScope
OkHttpClient provideOkHttpClientV2(
HttpLoggingInterceptor logging,
Interceptor headerInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();

//Removed custom timeout units
return builder.addInterceptor(headerInterceptor)
.addInterceptor(logging)
.retryOnConnectionFailure(true)
.readTimeout(100, TimeUnit.SECONDS)
.connectTimeout(100, TimeUnit.SECONDS)
.build();

}

@Provides
@ApplicationScope
Interceptor provideRetrofitHeaderV2(final SharedPreferencesUtil sharedPreferencesUtil) {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();

Request.Builder builder = original.newBuilder();
builder.header("Content-Type", "application/json")
.method(original.method(), original.body());
if (sharedPreferencesUtil != null) {
if (sharedPreferencesUtil.isLogin()) {

String loginToken = sharedPreferencesUtil.getLoginToken();
builder.addHeader("Authorization", "Bearer " + loginToken);
}
}

builder.addHeader("Connection", "close");

Request request = builder.build();
return chain.proceed(request);
}
};
}

@Provides
@ApplicationScope
HttpLoggingInterceptor provideLoggingInterceptorV2() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
if (BuildConfig.DEBUG) {
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
} else {
logging.setLevel(HttpLoggingInterceptor.Level.NONE);
}
return logging;
}

@Provides
@ApplicationScope
Retrofit provideRetrofitV2(OkHttpClient okHttpClient) {
return new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.build();
}```

我们已经尝试过的事情:

  • 使用 addHeader("Connection", "close") 添加标题
  • 添加连接池限制理想连接数
  • 增加超时时间

任何帮助将不胜感激,我们现在面临这个问题已经有一段时间了。

最佳答案

我刚遇到这种情况,想出了一个解决方法。这是由使用 HttpLoggingInterceptor 引起的。通过调试 okhttp 代码,我发现它抛出了那个错误,因为正文的长度与响应中的内容长度 header 不匹配。如果它接收到内容 header ,它会使用 FixedLengthSource,并且在读取时会抛出“流的意外结束”错误。你可以在源码中看到这个https://github.com/square/okhttp/blob/okhttp_3.9.x/okhttp/src/main/java/okhttp3/internal/http1/Http1Codec.java openResponseBody 方法

作为一种变通方法,我向我的构建器添加了一个response 解释器,该解释器去除了导致它使用 UnknownLengthSource 的内容长度 header 。

 private static final Interceptor REWRITE_CONTENT_LENGTH_INTERCEPTOR = new Interceptor() {
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().removeHeader("Content-Length")
.build();
}
};

关于android - HTTP FAILED : java. io.IOException: 发出 https 请求时流异常的意外结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55473516/

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