【发布时间】:2019-04-02 11:19:41
【问题描述】:
我们以前使用 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")添加标题 - 添加连接池限制理想连接
- 增加超时时间
我们将不胜感激任何帮助,我们已经面临这个问题很长一段时间了。
【问题讨论】:
-
似乎是 OkHttp 问题 github.com/square/okhttp/issues/2738
-
已经检查过大多数答案都不适用于我们的案例。
-
@Asheesh 我用真机运行,一切都会好起来的。
标签: android retrofit okhttp okhttp3