HTTPS加载图片(Android)

g82*_*435 0 https android image

对不起我的英语不好。我需要从网址加载图片,但网址使用HTTPS协议。我尝试通过android libruary ImageLoader加载图像,但出现错误:

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.
Run Code Online (Sandbox Code Playgroud)

我如何在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);
Run Code Online (Sandbox Code Playgroud)

并在适配器中

imageLoader.displayImage(image.get(position).getLinkImge(),holder.image);
Run Code Online (Sandbox Code Playgroud)

g82*_*435 6

它的工作,需要创建类

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) {
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

并像这样使用:

SSLCertificateHandler.nuke();
Run Code Online (Sandbox Code Playgroud)

及其工作=)