D / OkHttp:<-HTTP失败:javax.net.ssl.SSLException:SSL握手中止:ssl = 0x64e3c938:系统调用期间I / O错误,对等方重置连接

时间:2018-12-11 07:14:33

标签: java android ssl retrofit retrofit2

  

D / OkHttp:<-HTTP失败:javax.net.ssl.SSLException:SSL握手中止:ssl = 0x64e3c938:系统调用期间I / O错误,对等方重置连接

我在Android 4.2.2设备上收到此错误。在其他设备上运行相同的应用程序时,它可以正常工作。 请帮忙。

public static Retrofit getClient(final Context context, final Server newServer) {
    if(retrofit == null || server == null || !getServer().equals(newServer) || tok != null) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder okHttpClient = new OkHttpClient()
            .newBuilder().addInterceptor(loggingInterceptor);

        okHttpClient.addInterceptor( new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                  String hh=tok.replace("\"", "");  //For removing the " from the token
                    Request newRequest = chain
                            .request()
                            .newBuilder()
                            .addHeader(HTTP_AUTH_HEADER,"Bearer " + hh) //token use for the Authentication.
                            .build();
                    return chain.proceed(newRequest);
                }
            });


        retrofit = new Retrofit.Builder()
                .baseUrl(getBaseUrl(context, server))
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient.build())
                .build();
    }
    return retrofit;
}
private static String getBaseUrl(Context context, Server newServer) {
    StringBuilder builder = new StringBuilder();
    server = newServer; // update server address
    if(server != null && server.getAddress() != null) {
        return builder.append(server.getAddress()).toString();
    } else { // set default address
        return builder.append(BuildConfig.SERVER_ADDRESS).toString();
    }
}
}

1 个答案:

答案 0 :(得分:1)

Android 4.4和更低版本的设备没有默认的TLSv1.2支持。因此,您需要手动制作。

首先写下以下方法。

    public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
    if (Build.VERSION.SDK_INT < 22) {
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            client.sslSocketFactory(new Tls12SocketFactory(sc.getSocketFactory()), (X509TrustManager) trustAllCerts[0]);

            ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_2)
                    .build();

            ConnectionSpec csslv3 = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.SSL_3_0)
                    .build();

            List<ConnectionSpec> specs = new ArrayList<>();
            specs.add(cs);
            specs.add(csslv3);
            specs.add(ConnectionSpec.COMPATIBLE_TLS);
            specs.add(ConnectionSpec.CLEARTEXT);

            client.connectionSpecs(specs);
        } catch (Exception exc) {
            Log.e("OkHttpTLSCompat", "Error while setting TLS 1.2", exc);
        }
    }
    return client;
}

现在,在OkHttpClient.Builder okHttpClient = new OkHttpClient().newBuilder().addInterceptor(loggingInterceptor);这一行之后,添加以下代码。

okHttpClient = enableTls12OnPreLollipop(okHttpClient);

我希望它能解决您的问题。