2015-09-04 55 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

工作給我!不錯!謝謝!! –