图片加载失败出现如下问题,原因就是glide加载Https图片没有证书无法显示!
Glide无法显示问题解决思路如下:
Glide.with(this).load("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.listener(mRequestListener)
.into(imageView);
注意加粗字体!给glide配置监听措施!
RequestListener mRequestListener = new RequestListener() {
@Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
Log.d(TAG, "onException: " + e.toString()+" model:"+model+" isFirstResource: "+isFirstResource);
imageView.setImageResource(R.mipmap.ic_launcher);
return false;
}
@Override public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
Log.e(TAG, "model:"+model+" isFirstResource: "+isFirstResource);
return false;
}
};
打印出现的问题!
这里我想插一句题外话,安卓中我最看重的两点并没有多高深,其一为各种生命周期形态,其二为debug问题排查能力。
SSLHandshake问题解决思路如下:
在Application中配置全局忽略证书:
/**
* 忽略https的证书校验
* 避免Glide加载https图片报错:
* javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
*/
public static void handleSSLHandshake() {
try {
TrustManager[] trustAllCerts =new TrustManager[]{new X509TrustManager() {
public X509Certificate[]getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
SSLContext sc = SSLContext.getInstance("TLS");
// trustAllCerts信任所有的证书
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}catch (Exception ignored) {
}
}
然后在你项目的Application中调用全局忽略证书方法,就大功告成了。
诸君好运!
网友评论