毕加索不会下载有效的网址

时间:2018-07-24 01:18:30

标签: java android kotlin picasso android-glide

当我尝试从网站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中打开相同的链接,则该链接可以正常工作。

1 个答案:

答案 0 :(得分:2)

毕加索不支持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);

希望它可以帮助您解决问题!