okhttp3.internal.http2.StreamResetException: stream was reset: INTERNAL_ERROR

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;
    }

猜你喜欢

转载自blog.csdn.net/CHITTY1993/article/details/78402346