哟嘿 2019-10-10 15:17 采纳率: 50%
浏览 1952

javax.net.ssl.SSLException: Read error: ssl=0x56e63588: I/O error during system call, Connection reset by peer

不止出现在Android版本低的机型上 查了好多博客 有的说是服务器的原因 但是我觉得应该还是代码原因 请大神指教

private H buildService(int time, boolean isOpenCache) {
    //手动创建一个OkHttpClient并设置超时时间
    OkHttpClient.Builder builder = new OkHttpClient.Builder();

    //日志打印
    if (isDebugModel) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(httpLoggingInterceptor);
    }
    //添加header
     String token =getAppToken();/*getAppToken()*/
    if (!TextUtils.isEmpty(token)) {
        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("token", token)
                        .build();
                return chain.proceed(request);
            }
        });
    }
    if (isOpenCache) {
        builder.cache(cache);
        builder.addInterceptor(new CacheInterceptor());
        builder.addNetworkInterceptor(new CacheInterceptor());
    }
    if (time > 0) {
        if (time == 6)
            time = 10;
        DEFAULT_TIMEOUT = time;
    }
    builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
    builder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
    builder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
    builder.sslSocketFactory(createSSLSocketFactory());
    builder.hostnameVerifier(new TrustAllHostnameVerifier());

    OkHttpClient httpClient = builder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .client(httpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl(baseUrl)
            .build();
    return retrofit.create(hClass);
}
private static class TrustAllCerts implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
}

private static class TrustAllHostnameVerifier implements HostnameVerifier {
    @Override   public boolean verify(String hostname, SSLSession session) {
        return true;    }
}

private static SSLSocketFactory createSSLSocketFactory() {
    SSLSocketFactory ssfFactory = null;
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null,  new TrustManager[] {
                new TrustAllCerts() }, new SecureRandom());
        ssfFactory = sc.getSocketFactory();
    } catch (Exception e) {

    }
    return ssfFactory;
}

  • 写回答

2条回答 默认 最新

  • zqbnqsdsmd 2019-10-10 18:34
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化