gpt4 book ai didi

java - Picasso 不会下载有效的 URL

转载 作者:行者123 更新时间:2023-11-30 02:05:35 27 4
gpt4 key购买 nike

当我尝试从网站FilmAffinity下载图像时它不起作用,但对于其他网站的其他图像却可以。我使用的代码是

    Picasso.get().load("https://pics.filmaffinity.com/mamma_mia_here_we_go_again-440727588-mmed.jpg").into(imageView)

如果我使用回调,我会看到以下错误:com.squareup.picasso.NetworkRequestHandler$ResponseException: HTTP 403

用户似乎无权访问该网站,但如果我尝试在 Chrome 中打开相同的链接,它会完美地工作。

最佳答案

Picasso 不支持 https 方式。您可以引用一下方式:首先,您可以导入:

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'

并创建自定义类:

    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;
}
}

你可以使用它:

PicassoTrustAll.getInstance(context)
.load(url)
.into(imageView);

希望对您的问题有所帮助!

关于java - Picasso 不会下载有效的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51489155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com