毕加索不加载在ImageView的照片 [英] Picasso not loading pictures in ImageView
问题描述
我想为了让懒加载图像在ListView使用的毕加索。但是,它实际上不工作。在我的自定义适配器,我得到的ImageView的,初始化毕加索对象并指出将图像加载到指定的ImageView的。
从服务器检索图片,我需要提供基本的身份验证,所以我创建了一个自定义拦截它增加了认证头。
此外,我需要的,因为在目前的证书没有签署信任每一个SSL证书。
我面对的行为:
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:
- 验证错误 - >我已经添加了标题。该错误会消失。
- SSL证书信任的错误 - >我已经添加了一个SSLSocketFactory的,我用我所有的凌空请求相同。该错误会消失。
不过还是任何图像加载现在看来任何错误。
But still any pictures are loading and now any errors appears.
上面,code,它构建毕加索对象:
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();
}
然后,我如何使用毕加索对象变成我的适配器的getView方法:
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");
}
});
下面是膨胀的布局,其中包含其中图像已被加载的ImageView的
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
推荐答案
这似乎太明显的错误,但你用一个空毕加索的参考,而不是使用毕加索毕加索= getPicassoDownloader()的;
。我有一个类似的问题,并使用你的榜样和<一个解决它href=\"http://stackoverflow.com/questions/26126223/how-to-add-authentication-token-in-header-in-picasso-library\">How添加验证令牌头毕加索库。
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.
这篇关于毕加索不加载在ImageView的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!