I'm using the Picasso library version 2.71828 to load some image, but it does not work with all URL. Here is my code:
Picasso.get().load(url).into(imageView);
url2
: http://images.foody.vn/res/g14/138986/prof/s576x330/foody-mobile-a2-jpg-261-635682356468932282.jpg
url3
: https://static3.mytour.vn/resources/pictures/hotels/19/large_vlj1419841660_khach-san-gia-han.JPG
Picasso only works with url1
and url2
. It does not display image with url3
even I can open this on browsers.
Why can I load url3
with Picasso? Which types of url that Picasso does not load?
Picasso directly doesn't support https. so u have to combine this package.
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.picasso:picasso:2.4.0'
and add picasso custom class to handle https.
public class PicassoTrustAll {
private static Picasso mInstance = null;
private PicassoTrustAll(Context context) {
OkHttpClient client = new OkHttpClient();
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
});
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] x509Certificates,
String s) throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] x509Certificates,
String s) throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
} };
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
client.setSslSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
mInstance = new Picasso.Builder(context)
.downloader(new OkHttpDownloader(client))
.listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Log.e("PICASSO", exception);
}
}).build();
}
public static Picasso getInstance(Context context) {
if (mInstance == null) {
new PicassoTrustAll(context);
}
return mInstance;
}
}
Finally use class like That.
PicassoTrustAll.getInstance(context)
.load(url)
.into(imageView);