【发布时间】:2018-04-17 02:24:14
【问题描述】:
我正在尝试让它在 Oreo 设备上运行。这在旧设备上完美运行,但在奥利奥上却不行。 我在握手时遇到问题,所以我添加了
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
.build();
还有
.connectionSpecs(Collections.singletonList(spec))
在客户端。 完整代码:
private static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
.build();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectionSpecs(Collections.singletonList(spec))
.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;
}
但现在我遇到了错误
D/OkHttp:
D/错误:无法找到可接受的协议。 isFallback=false, 模式=[连接规范(密码套件=[TLS_DHE_RSA_WITH_AES_256_CBC_SHA], tlsVersions=[TLS_1_3],支持TlsExtensions=true)],支持 协议=[TLSv1, TLSv1.1, TLSv1.2]
有什么建议吗?
编辑 现在我已经这样做了,它可以工作。
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
}else {
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
.build();
client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectionSpecs(Collections.singletonList(spec))
.build();
}
【问题讨论】:
标签: android retrofit2 okhttp android-8.0-oreo