本文介绍了从Skyscanner API检索会话密钥时出现javax.net.ssl.SSLPeerUnverifiedException错误-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用 Sky扫描仪API 来检索android中的机票价格和其他详细信息.
I have been working on Sky scanner API to retrieve the flight fares and other details in android.
我正在使用 Retrofit2 进行网络通话.问题是在获取会话密钥时出现错误
I'm using the Retrofit2 for network calls. The problem is getting error in retrieving the Session key
错误
javax.net.ssl.SSLPeerUnverifiedException: Hostname partners.api.skyscanner.net not verified:certificate: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=DN:CN=api.skyscanner.net,OU=Infrastructure,O=Skyscanner Ltd,L=London,ST=London,C=GB
APIClient
public static Retrofit getRetrofit() {
if (retrofit2 == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify("partners.api.skyscanner.net", session);
}
}).build();
retrofit2 = new Retrofit.Builder()
.baseUrl(SKY_BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit2;
}
android中SSLPeerUnverifiedException的引用的SO链接
Referred SO links of SSLPeerUnverifiedException in android
推荐答案
我借助 BNK 评论
- 在OkhttpClient对象中添加
HostnameVerifier()
- 检查主机名,然后再将其添加到
hv.verify(hostname, session);
固定代码以供参考
if (retrofit2 == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify("api.skyscanner.net", session);
}
}).build();
retrofit2 = new Retrofit.Builder()
.baseUrl(SKY_BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
这篇关于从Skyscanner API检索会话密钥时出现javax.net.ssl.SSLPeerUnverifiedException错误-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!