为什么无法在Android中加载此Url中的图片

时间:2014-08-28 04:18:35

标签: java android http android-imageview android-volley

我一直试图在此Url加载图像好几天,但我尝试过的任何事情都没有成功。我正在使用Volley库,我尝试的第一件事就是简单......

        mImageView.setImageUrl("https://cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg", VolleyImgController.getInstance().getImageLoader());

但是在networkimageview中返回了一个空白。 以及logcat中的以下消息

08-27 22:33:49.209    3069-3069/com.supervalu.mobile.android D/Volley﹕ [1] Request.finish: 15308 ms: [ ] https://cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg 0x422b54c3 LOW 1

然后我尝试创建自己的HttpStack,以防它是使用以下代码加载https图像的问题......

public static DefaultHttpClient getHttpsClient(){
    // register scheme for http
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http",new PlainSocketFactory(),80));
    //register scheme for https
    SocketFactory https_socket = new FakeSocketFactory();
    registry.register(new Scheme("https", https_socket,433));

    HttpParams httpParams = new BasicHttpParams();
    //setting connection timeout.
    HttpConnectionParams.setConnectionTimeout(httpParams, MyConstants.CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, MyConstants.CONNECTION_TIMEOUT);
    return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams,registry),httpParams);

}

然后我将其传递到此代码块

            requestQueue = Volley.newRequestQueue(getApplicationContext(),new HttpClientStack(HttpClient.getHttpsClient()));

这对我也没有任何帮助,我得到了与之前相同的回应。

然后我从https://github.com/ogrebgr/android_volley_examples下载了一个凌空示例项目,看看该url是否可以在他们的项目中运行,并且当我插入它时它也没有加载图像。

我的问题是,为什么我无法显示此网址的图片? 有什么办法可以解决这个问题吗?

=====编辑===== Url Variants我试过了 http://cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg

https://cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg

https://www.cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg

这些都会回复301重定向到此网址https://www.cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg

我认为问题可能与url字符串无关。有人能够在他们的Android应用程序中从这个URL加载图像吗?如果是这样,你是怎么做到的?

2 个答案:

答案 0 :(得分:0)

我通过chrome访问网址,它说这不是秘密链接。为什么不尝试将网址更改为https://www.cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg

PS:只需将www添加到网址标题。

更新: 我使用Android-Universal-Image-Loader并查找Document,图片已加载!!!

enter image description here

PS:也许你需要网络ca:openssl s_client -connect cub.com:443

-----BEGIN CERTIFICATE-----
MIIFRjCCBC6gAwIBAgIQSU8lG29bDd4CR2JehPYubDANBgkqhkiG9w0BAQUFADCB
tTELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL
ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1c2Ug
YXQgaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JwYSAoYykxMDEvMC0GA1UEAxMm
VmVyaVNpZ24gQ2xhc3MgMyBTZWN1cmUgU2VydmVyIENBIC0gRzMwHhcNMTMwMzA4
MDAwMDAwWhcNMTYwMzA3MjM1OTU5WjB4MQswCQYDVQQGEwJVUzESMBAGA1UECBMJ
TWlubmVzb3RhMRUwEwYDVQQHFAxFZGVuIFByYWlyaWUxFzAVBgNVBAoUDlNVUEVS
VkFMVSBJTkMuMQswCQYDVQQLFAJJVDEYMBYGA1UEAxQPcmV3YXJkcy5jdWIuY29t
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlTBhdBk0FHmVmr0enoab
19ypUxvF+C5+ITCHiY9w4nFWF7BkGzfY3DQ7NJdTK23R8XMTNW3kaZp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w7wadhKUqS9+2dMEE7A1JNhE44TWC/C3kTHa+P
ww0ZhHlmqAO2wAmMk+Q6iCt4Fa+rLENgvtXvoRRM0ngP6RUeEQ80+cTnHv22s8N3
8PaBlDkOCYLySA==
-----END CERTIFICATE-----

以下是我用来替换BaseImageDownLoader的代码:

    protected HttpsURLConnection createConnection(String url, Object extra) throws IOException {
    String encodedUrl = Uri.encode(url, ALLOWED_URI_CHARS);

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = null;
    try {
        cf = CertificateFactory.getInstance("X.509");
    } catch (CertificateException e1) {

        // TODO Auto-generated catch block
        e1.printStackTrace();

    }
    // From https://www.washington.edu/itconnect/security/ca/load-der.crt
    InputStream caInput = new BufferedInputStream(new FileInputStream(Environment.getExternalStorageDirectory()
            + "/cub.crt"));
    Certificate ca = null;
    try {
        try {
            ca = cf.generateCertificate(caInput);
            System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
        } catch (CertificateException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    } finally {
        caInput.close();
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = null;
    try {
        keyStore = KeyStore.getInstance(keyStoreType);
        try {
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
        } catch (NoSuchAlgorithmException e1) {

            // TODO Auto-generated catch block
            e1.printStackTrace();

        } catch (CertificateException e1) {

            // TODO Auto-generated catch block
            e1.printStackTrace();

        }

        // Create a TrustManager that trusts the CAs in our KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = null;
        try {
            tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);
        } catch (NoSuchAlgorithmException e1) {

            // TODO Auto-generated catch block
            e1.printStackTrace();

        }

        // Create an SSLContext that uses our TrustManager
        SSLContext context = null;
        try {
            context = SSLContext.getInstance("TLS");
        } catch (NoSuchAlgorithmException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        try {
            context.init(null, tmf.getTrustManagers(), null);
        } catch (KeyManagementException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        HttpsURLConnection conn = (HttpsURLConnection) new URL(encodedUrl).openConnection();
        conn.setConnectTimeout(connectTimeout);
        conn.setSSLSocketFactory(context.getSocketFactory());
        conn.setReadTimeout(readTimeout);
        return conn;
    } catch (KeyStoreException e) {

        // TODO Auto-generated catch block
        e.printStackTrace();

    }
    return null;

}

答案 1 :(得分:-1)

我在chrome中试过这个,它无法加载由" https"

尝试使用http架构

http://www.cub.com/content/dam/B2C/enterprisebannerwebsites/Cub/CUB_WK25_HP_BackToSchool.jpg