During the development of a project, it was found that using universal-image-loader could not load the HTTPS picture resource and view the log found that the certificate check failed.
View source discovery, Imageloader can be overridden by inheriting baseimagedownloader to download some of the logic, to achieve the following
1 Public classAuthimagedownloaderextendsBaseimagedownloader {2 3 Public Static FinalString TAG = Authimagedownloader.class. GetName ();4 5 /**6 * {@value}7 */8 Public Static Final intDefault_http_connect_timeout = 5 * 1000;//milliseconds9 /**Ten * {@value} One */ A Public Static Final intDefault_http_read_timeout = 20 * 1000;//milliseconds - - PublicAuthimagedownloader (Context context) { the This(context, default_http_connect_timeout, default_http_read_timeout); - } - - PublicAuthimagedownloader (Context context,intConnectTimeout,intreadtimeout) { + Super(context, connecttimeout, readtimeout); - } + A @Override at protectedInputStream getstreamfromnetwork (String Imageuri, Object Extra)throwsIOException { - -URL url =NULL; - Try { -URL =NewURL (Imageuri); -}Catch(malformedurlexception e) { in log.e (TAG, E.getmessage (), e); - } toHttpURLConnection http =NULL; + - if(Scheme.ofuri (imageuri) = =Scheme.https) { the trustallhosts (); *Httpsurlconnection HTTPS =(httpsurlconnection) URL $ . OpenConnection ();Panax Notoginseng Https.sethostnameverifier (do_not_verify); -HTTP =https; the Http.connect (); +}Else { AHTTP =(HttpURLConnection) url.openconnection (); the } + - http.setconnecttimeout (connecttimeout); $ http.setreadtimeout (readtimeout); $ return NewFlushedinputstream (NewBufferedinputstream ( - Http.getinputstream ())); - } the - //Always verify the Host-dont check for certificateWuyi Final StaticHostnameverifier do_not_verify =NewHostnameverifier () { the @Override - Public BooleanVerify (String hostname, sslsession session) { Wu return true; - } About }; $ - /** - * Trust every server-dont check for any certificate - */ A Private Static voidtrustallhosts () { + //Create A trust manager that does not validate certificate chains thetrustmanager[] Trustallcerts =Newtrustmanager[]{NewX509trustmanager () { - @Override $ Public voidcheckclienttrusted ( the java.security.cert.x509certificate[] X509Certificates, theString s)throwsjava.security.cert.CertificateException { the } the - @Override in Public voidcheckservertrusted ( the java.security.cert.x509certificate[] X509Certificates, theString s)throwsjava.security.cert.CertificateException { About } the the @Override the Publicjava.security.cert.x509certificate[] Getacceptedissuers () { + return Newjava.security.cert.x509certificate[]{}; - } the }};Bayi the //Install the All-trusting trust manager the Try { -Sslcontext sc = sslcontext.getinstance ("TLS"); -Sc.init (NULL, Trustallcerts,Newjava.security.SecureRandom ()); the httpsurlconnection the . Setdefaultsslsocketfactory (Sc.getsocketfactory ()); the}Catch(Exception e) { the e.printstacktrace (); - } the } the}
View Code
Then configure the Imageloader in the
Imageloaderconfiguration.builder config = new Imageloaderconfiguration.builder ( context);.. Config.imagedownloader (New Authimagedownloader (this));
Problem solving, OK
Resolve Imageloader load HTTPS picture certificate Check exception problem