okhttp3.internal.http2.StreamResetException: stream was reset: INTERNAL_ERROR
阿新 • • 發佈:2019-02-04
BUG:
okhttp3.internal.http2.StreamResetException: stream was reset: INTERNAL_ERROR
原因是協議錯誤導致的,在例項化 okhttpclient 的時候用以下方法:
okHttpClient = httpBuilder .protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
完整程式碼貼下:
private static OkHttpClient okHttpClient = null ;
private static final int DEFAULT_TIMEOUT = 30;// 20
private static Context mContext;
public static OkHttpClient getOkHttpSingletonInstance() {
if (okHttpClient == null) {
synchronized (OkHttpClient.class) {
if (okHttpClient == null) {
okHttpClient = new OkHttpClient();
//設定合理的超時
OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder()
.readTimeout(3, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS) //設定連線超時 30秒
.writeTimeout(3 , TimeUnit.MINUTES)
.addInterceptor(new LoggingInterceptor())//新增請求攔截
.retryOnConnectionFailure(true);
//如果不是在正式包,新增攔截 列印響應json
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(
new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
if (TextUtils.isEmpty(message)) return;
String s = message.substring(0, 1);
//如果收到響應是json才打印
if ("{".equals(s) || "[".equals(s)) {
LogUtils.i("收到響應: " + message);
}
}
});
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
httpBuilder.addInterceptor(logging);
}
okHttpClient = httpBuilder
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
}
}
}
return okHttpClient;
}