2015-09-04 64 views
0

对不起,我的英语。我需要从url加载图片,但url使用HTTPS协议。我试图通过Android的libruary ImageLoader负荷的形象和我有错误:HTTPS加载图像(android)

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 
    javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 

我怎样才能在协议HTTPS加载图像?

我的例子:

主要活动

DisplayImageOptions mDisplayImageOptions = new DisplayImageOptions.Builder() 
       .showImageForEmptyUri(R.drawable.abc_ab_share_pack_mtrl_alpha) 
       /*.showImageOnLoading(R.drawable.loading_bg) 
       .showImageOnLoading(R.drawable.loading_bg)*/ 
       .cacheInMemory(true) 
       .cacheOnDisc(true) 
       .build(); 

     ImageLoaderConfiguration conf = new ImageLoaderConfiguration.Builder(context) 
       .defaultDisplayImageOptions(mDisplayImageOptions) 
       .memoryCacheSize(50 * 1024 * 1024) 
       .discCacheSize(50 * 1024 * 1024) 
       .denyCacheImageMultipleSizesInMemory() 
       .diskCacheExtraOptions(250, 250, null) 
       .threadPoolSize(5) 
       .writeDebugLogs() 
       .build(); 


     mImageLoader = ImageLoader.getInstance(); 
     mImageLoader.init(conf); 

而且在适配器

imageLoader.displayImage(image.get(position).getLinkImge(),holder.image); 
+1

**服务器的SSL证书不是由受信任的机构签署的**。您需要服务器的* valid *证书或为自定义图像下载类实现自定义SSLSocketFactory,如果满足某些条件,将绕过证书检查。 –

+1

你使用自签名证书吗? – Panther

+1

检查了这一点,如果它可以帮助你的问题 http://stackoverflow.com/questions/21183043/sslhandshakeexception-trust-anchor-for-certification-path-not-found-android-http – Panther

回答

2

的工作,需要创建类

public class SSLCertificateHandler { 

    protected static final String TAG = "NukeSSLCerts"; 

    /** 
    * Enables https connections 
    */ 
    public static void nuke() { 
     try { 
      TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
       public X509Certificate[] getAcceptedIssuers() { 
        X509Certificate[] myTrustedAnchors = new X509Certificate[0]; 
        return myTrustedAnchors; 
       } 

       @Override 
       public void checkClientTrusted(X509Certificate[] certs, String authType) { 
       } 

       @Override 
       public void checkServerTrusted(X509Certificate[] certs, String authType) { 
       } 
      } }; 

      SSLContext sc = SSLContext.getInstance("SSL"); 
      sc.init(null, trustAllCerts, new SecureRandom()); 
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
      HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { 
       @Override 
       public boolean verify(String arg0, SSLSession arg1) { 
        return true; 
       } 
      }); 
     } catch (Exception e) { 
     } 
    } 

} 

,并使用这样的:

SSLCertificateHandler.nuke(); 

及其工作=)

+0

工作给我!不错!谢谢!! –