并且在android手机中的浏览器上访问该图片会提示网站安全证书已过期或不可信是否继续浏览的提示,如图:
解决方案一
设置请求支持https:
我们应该实现 ImageDownloader ,然后把它设置给ImageLoader的configuration
代码如下:
public class AuthImageDownloader extends BaseImageDownloader { private SSLSocketFactory mSSLSocketFactory; public AuthImageDownloader(Context context) { super(context); SSLContext sslContext = sslContextForTrustedCertificates(); mSSLSocketFactory = sslContext.getSocketFactory(); } public AuthImageDownloader(Context context, int connectTimeout, int readTimeout) { super(context, connectTimeout, readTimeout); SSLContext sslContext = sslContextForTrustedCertificates(); mSSLSocketFactory = sslContext.getSocketFactory(); } @Override protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException { URL url = null; try { url = new URL(imageUri); } catch (MalformedURLException e) { } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(connectTimeout); conn.setReadTimeout(readTimeout); if (conn instanceof HttpsURLConnection) { ((HttpsURLConnection)conn).setSSLSocketFactory(mSSLSocketFactory); ((HttpsURLConnection)conn).setHostnameVerifier((DO_NOT_VERIFY)); } return new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE); } // always verify the host - dont check for certificate final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; }
public SSLContext sslContextForTrustedCertificates() { javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1]; javax.net.ssl.TrustManager tm = new miTM(); trustAllCerts[0] = tm; SSLContext sc = null; try { sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, null); //javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }catch (KeyManagementException e) { e.printStackTrace(); }finally { return sc; } }
class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public boolean isServerTrusted( java.security.cert.X509Certificate[] certs) { return true; } public boolean isClientTrusted( java.security.cert.X509Certificate[] certs) { return true; } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; } }
ImageLoaderConfiguration config = new ImageLoaderConfiguration .Builder(getApplicationContext()) .defaultDisplayImageOptions(defaultOptions) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .imageDownloader(new AuthImageDownloader(this)) .tasksProcessingOrder(QueueProcessingType.LIFO).build();