最近在写做一个Android TV的项目,其中有一个功能是获取天气。在模拟器上测试时,是可用的。但当我在电视盒子上运行时出现了问题(报了SSLHandshakeExceptiion)。这里我访问网络用的是zhy的OKHttpUtils,于是我查阅了许多资料。发现是证书没有被信任的原因,但是我所访问的是心知天气的接口,所以应该不是接口问题。而且我是通过ip去获取本地天气的(在之前通过网络访问获取的网络ip)。在我一筹莫展的时候,我发现了这篇文章:
http://blog.csdn.net/Kenway090704/article/details/73817700
这里面提到,出现问题的可能有两种:
1,时间问题。
2,证书不被信任。
于是我查看了电视盒子的时间,时2016年的某一天,所以我判断是时间问题。但是,盒子的时间是通过网络获取的,而且我无法修改。像我这种码农,人微言轻的,也不方便让公司换个盒字给我测试。于是我使用了上文中的第二个办法,修改OKHttpClient的创建方式信任该证书,以得到天气情况。这里我做了简单的封装。
下面是代码:
**
* 网络获取中证书出现问题时,无法正常返回数据。(有可能是因为时间问题)
* Created by sjw on 2017/10/24.
*/
public class OkHttpHelper implements Runnable {
private OkHttpClient okHttpClient;
private WeatherBackListener weatherBackListener;
private String url;
private String response;
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
weatherBackListener.onWeatherBack(response);
return false;
}
});
@Override
public void run() {
okHttpClient = getUnsafeOkHttpClient();
Request request = new Request.Builder().url(url).build();
try {
Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
this.response = response.body().string();
handler.sendEmptyMessage(1);
} else {
this.response="失败";
}
} catch (IOException e) {
this.response="失败";
e.printStackTrace();
}
}
public interface WeatherBackListener {
void onWeatherBack(String response);
}
public void getWeather(String url, WeatherBackListener weatherBackListener) {
this.url = url;
this.weatherBackListener = weatherBackListener;
new Thread(this).start();
}
private static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
这里是调用代码的方法:
OkHttpHelper okHttpHelper = new OkHttpHelper();
okHttpHelper.getWeather(url, weatherBackListener);
网友评论