Hello I am using the Picasso
library to download the images from URL.
URL : https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t1.0-1/s200x200/14728
final OkHttpClient client = new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
Picasso picasso = new Picasso.Builder(mContext)
.downloader(new OkHttp3Downloader(client))
.build();
picasso .with(mContext).load(YOUR_URL).into(YOUR_IMAGEVIEW);
Add
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
Use those dependencies in your Gradle
:
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 this class instead of the original Picasso
class
Picasso class:
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;
}
}
Usage example:
PicassoTrustAll.getInstance(context)
.load(url)
.into(imageView);
Just put this this code in your Application class:
final OkHttpClient client = new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
final Picasso picasso = new Picasso.Builder(this)
.downloader(new OkHttp3Downloader(client))
.build();
Picasso.setSingletonInstance(picasso);
from this link check it.
The following works fine for these dependencies:
implementation 'com.squareup.okhttp3:okhttp:3.0.1'
implementation 'com.squareup.picasso:picasso:2.71828'
First define a client to accept certificates:
public class SelfSignCertHttpClient {
public static OkHttpClient getSelfSignOkHttpClient(List<Interceptor> interceptors) {
try {
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
for (Interceptor interceptor : interceptors) {
builder.addInterceptor(interceptor);
}
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Source: https://futurestud.io/tutorials/retrofit-2-how-to-trust-unsafe-ssl-certificates-self-signed-expired
I have also added the option to pass a list of interceptors when creating the client.
You can than define a PicasoTrustAll class:
public class PicassoTrustAll {
private static Picasso mInstance = null;
private PicassoTrustAll(Context context) {
List<Interceptor> interceptors = new ArrayList<>();
// Add any interceptors you wish (e.g Authorization Header interceptor)
mInstance = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(SelfSignCertHttpClient.getSelfSignOkHttpClient(interceptors)))
.listener((picasso, uri, exception) -> Log.e("PICASSO", exception.getMessage())).build();
}
public static Picasso getInstance(Context context) {
if (mInstance == null) {
new PicassoTrustAll(context);
}
return mInstance;
}
}
Sample usage:
PicassoTrustAll.getInstance(this).load("https://10.0.2.2/path/to/picture").into(mainPictureView);
@nexus700120 solution is not up to date
@Neha Tyagi solution works but creates multiple instances of Picasso
in each instance of activity which leaks memory
So I crafted a up to date and singleton solution for redirecting image URLs -
CustomPicasso.java
import android.content.Context;
import android.util.Log;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
/**
* Created by Hrishikesh Kadam on 19/12/2017
*/
public class CustomPicasso {
private static String LOG_TAG = CustomPicasso.class.getSimpleName();
private static boolean hasCustomPicassoSingletonInstanceSet;
public static Picasso with(Context context) {
if (hasCustomPicassoSingletonInstanceSet)
return Picasso.with(context);
try {
Picasso.setSingletonInstance(null);
} catch (IllegalStateException e) {
Log.w(LOG_TAG, "-> Default singleton instance already present" +
" so CustomPicasso singleton cannot be set. Use CustomPicasso.getNewInstance() now.");
return Picasso.with(context);
}
Picasso picasso = new Picasso.Builder(context).
downloader(new OkHttp3Downloader(context))
.build();
Picasso.setSingletonInstance(picasso);
Log.w(LOG_TAG, "-> CustomPicasso singleton set to Picasso singleton." +
" In case if you need Picasso singleton in future then use Picasso.Builder()");
hasCustomPicassoSingletonInstanceSet = true;
return picasso;
}
public static Picasso getNewInstance(Context context) {
Log.w(LOG_TAG, "-> Do not forget to call customPicasso.shutdown()" +
" to avoid memory leak");
return new Picasso.Builder(context).
downloader(new OkHttp3Downloader(context))
.build();
}
}
build.gradle (Module:app)
android {
...
}
dependencies {
...
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
}
Usage -
CustomPicasso.with(context)
.load("http://i.imgur.com/DvpvklR.png")
.into(imageView);
For latest revisions check CustomPicasso gist - https://gist.github.com/hrishikesh-kadam/09cef31c736de088313f1a102f5ed3a3