带有Picasso的Android App无法使用HTTPS加载图像

时间:2019-02-14 16:40:45

标签: android picasso http-status-code-504

我将应用程序从HTTP更改为HTTPS,毕加索在HTTP上运行良好,但在HTTPS上却不工作,加载图像时出现 HTTP 504 错误。 我正在使用那些版本

implementation 'com.squareup.okhttp:okhttp:2.7.5'
implementation 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
implementation 'com.squareup.picasso:picasso:2.71828'

和这个助手:

public class PicassoTrustAll {
private static Picasso mInstance = null;

private PicassoTrustAll(Context context) {

    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, null);

        //OkHttpClient client = new OkHttpClient();

        OkHttpClient.Builder b = new OkHttpClient.Builder();
        b.readTimeout(30, TimeUnit.SECONDS);
        b.writeTimeout(30, TimeUnit.SECONDS);
        b.connectTimeout(30, TimeUnit.SECONDS);
        b.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                final Request.Builder newRequest = chain.request().newBuilder();
                newRequest.addHeader("header_key", "header_value");
                return chain.proceed(newRequest.build());
            }
        });


        OkHttpClient client = b.build();


        mInstance = new Picasso.Builder(context).downloader(new OkHttp3Downloader(client)).listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                Log.e("onImageLoadFailed", exception.getMessage());
            }
        }).build();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static Picasso getInstance(Context context) {
    if (mInstance == null) {
        new PicassoTrustAll(context);
    }
    return mInstance;
}

}

我还尝试如下配置安全性: 我创建了一个配置安全性xml文件

<?xml version="1.0" encoding="utf-8"?>
  <network-security-config>
      <base-config cleartextTrafficPermitted="true" />
      <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">172.20.10.6</domain>
      </domain-config>
</network-security-config>

并在manifest.xml中使用它:

android:networkSecurityConfig="@xml/network_security_config"

这一切都不起作用,有解决方案吗?

0 个答案:

没有答案