在一些Android设备上握手失败

时间:2018-04-06 10:10:30

标签: android rest https retrofit

在某些设备上,我与使用改造框架的服务器进行通信时没有任何问题。我还使用了几个Android版本来验证代码是否运行。但在某些设备(三星S8)上我每次都会收到错误:'握手失败'有谁知道问题出在哪里?谢谢!

这是我的代码:

protected static Retrofit getInstanceWithoutToken() {
    final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            final Request original = chain.request();
            final Request request = original.newBuilder()
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    });
    return new Retrofit.Builder()
            .baseUrl(CommonConstantsRest.REST_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();
}

1 个答案:

答案 0 :(得分:0)

问题是ssl通信。添加以下CipherSuite解决了问题

protected static Retrofit getInstanceWithoutToken() {
    ConnectionSpec spec = new
            ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
            .tlsVersions(TlsVersion.TLS_1_2)
            .cipherSuites(
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                    CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                    CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
            .build();
    final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
    httpClient.connectionSpecs(Collections.singletonList(spec));
    httpClient
            .addInterceptor(logging)
            .addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            final Request original = chain.request();
            final Request request = original.newBuilder()
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    });
    return new Retrofit.Builder()
            .baseUrl(CommonConstantsRest.REST_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();
}