Reputation: 2771
I'm trying to use Picasso in order to make a lazy picture loading in a ListView. But it's actually not working. In my custom adapter, I get the ImageView, initialize a Picasso object and indicate to load the image into the specified ImageView. To retrieve picture from server, I needed to provide a basic authentication, so I've created a custom interceptor which adds the authentication header. Moreover, I need to trust every SSL certificate because for the moment the certificate is not signed. The behaviors that I faced:
But still any pictures are loading and now any errors appears.
Above, the code that builds the Picasso Object:
public Picasso getPicassoDownloader() throws NoSuchAlgorithmException, KeyManagementException
{
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
@Override
public X509Certificate[] getAcceptedIssuers() {
X509Certificate[] myTrustedAnchors = new X509Certificate[0];
return myTrustedAnchors;
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
Picasso.Builder builder = new Picasso.Builder(getContext()).listener(new Listener() {
@Override
public void onImageLoadFailed(Picasso arg0, Uri arg1, Exception ex) {
ex.printStackTrace();
}
});
OkHttpClient client = new OkHttpClient();
client.setSslSocketFactory(sc.getSocketFactory());
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// TODO Auto-generated method stub
return true;
}
});
client.networkInterceptors().add(new BasicAuthInterceptor());
Downloader downloader = new OkHttpDownloader(client);
return builder.downloader(downloader).build();
}
Then, How I use the Picasso object into the getView method of my adapter:
ImageView imageView = (ImageView) convertView.findViewById(R.id.productImage);
Picasso picasso = null;
picasso.load(produit.getImageDefaultUri()).fit().into(imageView, new Callback(){
@Override
public void onError() {
System.out.println("Error");
}
@Override
public void onSuccess() {
System.out.println("Success");
}
});
Here is the inflated layout which contains the ImageView where the picture has to be loaded.
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/productImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:contentDescription="Product Image" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible"
android:indeterminateDrawable="@drawable/progressbar" >
</ProgressBar>
</RelativeLayout>
Would be great if somebody could help me with that ! Thanks in advance
Upvotes: 1
Views: 1022
Reputation: 103
This seems too obvious of a mistake, but you use a null Picasso reference instead of using Picasso picasso = getPicassoDownloader();
. I was having a similar issue and solved it using your example and How to add authentication token in header in Picasso library.
Upvotes: 1