Last active
July 17, 2018 15:34
-
-
Save huuphuoc1396/8e0e68098b1d27d860cb07927e94a981 to your computer and use it in GitHub Desktop.
Trust anchor for certification path not found when loading image from URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.certpathvalidator; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.view.animation.AccelerateInterpolator; | |
import android.view.animation.AlphaAnimation; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationSet; | |
import android.view.animation.AnimationUtils; | |
import android.widget.ImageView; | |
import com.loopj.android.http.AsyncHttpClient; | |
import com.loopj.android.http.AsyncHttpResponseHandler; | |
import java.security.KeyManagementException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.security.cert.X509Certificate; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.X509TrustManager; | |
import cz.msebera.android.httpclient.Header; | |
import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory; | |
public class ImageLoaderWithCertificate { | |
private ImageView imageView; | |
public ImageLoaderWithCertificate get(String url, final Listener listener) { | |
final Animation fadeIn = new AlphaAnimation(0, 1); | |
fadeIn.setInterpolator(new AccelerateInterpolator()); | |
fadeIn.setDuration(250); | |
AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); | |
asyncHttpClient.setSSLSocketFactory(new SSLSocketFactory(getSslContext(), | |
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)); | |
asyncHttpClient.get(url, new AsyncHttpResponseHandler() { | |
@Override | |
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { | |
final Bitmap bitmap = BitmapFactory.decodeByteArray(responseBody, 0, responseBody.length); | |
imageView.setImageBitmap(bitmap); | |
imageView.startAnimation(fadeIn); | |
listener.onSuccess(); | |
} | |
@Override | |
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, | |
Throwable error) { | |
listener.onFailure(error); | |
} | |
}); | |
return this; | |
} | |
public ImageLoaderWithCertificate into(ImageView imageView) { | |
this.imageView = imageView; | |
return this; | |
} | |
private SSLContext getSslContext() { | |
TrustManager[] byPassTrustManagers = new TrustManager[]{new X509TrustManager() { | |
public X509Certificate[] getAcceptedIssuers() { | |
return new X509Certificate[0]; | |
} | |
@SuppressLint("TrustAllX509TrustManager") | |
public void checkClientTrusted(X509Certificate[] chain, String authType) { | |
} | |
@SuppressLint("TrustAllX509TrustManager") | |
public void checkServerTrusted(X509Certificate[] chain, String authType) { | |
} | |
}}; | |
SSLContext sslContext = null; | |
try { | |
sslContext = SSLContext.getInstance("TLS"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
try { | |
if (sslContext != null) { | |
sslContext.init(null, byPassTrustManagers, new SecureRandom()); | |
} | |
} catch (KeyManagementException e) { | |
e.printStackTrace(); | |
} | |
return sslContext; | |
} | |
public interface Listener { | |
void onSuccess(); | |
void onFailure(Throwable error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment