UniversalImageLoader加载https图片

最近在开发项目中发现一个问题,由于我们项目中用的是比较早的一个开源图片加载框架UniversalImageLoader,服务器返回的图片是https开头的链接的时候会发现没办法加载图片。

这时候我们就需要通过设置uil支持https请求,uil里面有个类是下载图片ImageDownloader,这个类是关键,图片下载就是通过这个类来实现。我们需要通过实现ImageDownloader这个类,然后通过ImageLoaderConfiguration.imageDownloader(newAuthHttpsImageDownloader(mContext,5*1000,30*1000)) 这个来设置。代码如下:

importandroid.content.Context;

importcom.nostra13.universalimageloader.core.download.BaseImageDownloader;

importjava.io.BufferedInputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.net.HttpURLConnection;

importjava.net.MalformedURLException;

importjava.net.URL;

importjava.security.KeyManagementException;

importjava.security.NoSuchAlgorithmException;

importjavax.net.ssl.HostnameVerifier;

importjavax.net.ssl.HttpsURLConnection;

importjavax.net.ssl.SSLContext;

importjavax.net.ssl.SSLSession;

importjavax.net.ssl.SSLSocketFactory;

public class AuthHttpsImageDownloader extends BaseImageDownloader {

private SSLSocketFactory mSSLSocketFactory;

    public AuthHttpsImageDownloader(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;

    }

};

//设置ssl信任证书

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;

     }

  }

}


通过以上方法就可以解决https图片加载问题了

你可能感兴趣的:(UniversalImageLoader加载https图片)