android - 在某些 Android 设备上握手失败

标签 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();
}

最佳答案

问题出在 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();
}

关于android - 在某些 Android 设备上握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49690403/

相关文章:

android - 如何在相对布局内的图像上放置背景?

java - 如何保存用户上次阅读的页码

java - 如何在java中添加try和catch

java - GWT + Spring MVC(RESTful Web 服务)

ruby HTTPClient - SSL 和设置 _auth

java - 在java servlet中从http重定向到https并返回http

android - 无法启动 './qemu/linux-x86_64/qemu-system-i386' : No such file or directory

javascript - 基于 JavaScript/JSON 的 OData 调用如何进行身份验证?

javascript - 哪些浏览器允许使用 Access-Control-Allow-Origin : *? 进行跨域 ajax 调用

php - 如何确定您是否在没有 $_SERVER ['HTTPS' 的情况下使用 HTTPS]