【问题标题】:Volley NetworkImageView using httpsVolley NetworkImageView 使用 https
【发布时间】:2014-04-17 12:43:45
【问题描述】:

我在我的应用程序中使用 facebook 照片。 facebook 照片存储在 https url 后面。

谁能给我一个使用 https 使用 networkimageview 加载图像的示例?

【问题讨论】:

    标签: android image https android-volley


    【解决方案1】:

    我也有类似的问题,不是facebook,而是https下的图片。

    除了有一个自签名证书之外,还有很多重定向、cookie 管理等。所以我将 HttpClient Stack 与 Volley 一起使用,现在一切正常。

    也许这对您的问题有帮助。 您可以跳过所有您不感兴趣的部分。

    初始化HttpClient(不需要的可以跳过)

    // Create and initialize HTTP parameters
    HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, true );
    
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout( params, 5000 );
    
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout( params, 10000 );
    
    // The params are read in the ctor of the pool constructed by
    // ThreadSafeClientConnManager, and need to be set before constructing it.
    ConnManagerParams.setMaxTotalConnections(params, 15);
    ConnPerRoute cpr = new ConnPerRoute() {
       @Override
       public int getMaxForRoute(HttpRoute httpRoute) { return 5; }
    };
    
    ConnManagerParams.setMaxConnectionsPerRoute(params, cpr);
    
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    
    // Create and initialize scheme registry
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    
    /* Since I'm in a development environment I need to trust self-signed certs */
    SSLSocketFactory sslSocketFactory = null;
    try {
       X509TrustManager tm = new X509TrustManager() {
          public void checkClientTrusted(X509Certificate[] xcs, String string)
             throws CertificateException { }
    
          public void checkServerTrusted(X509Certificate[] xcs, String string)
             throws CertificateException { }
    
          public X509Certificate[] getAcceptedIssuers() { return null; }
       };
    
       SSLContext ctx = SSLContext.getInstance("TLS");
       ctx.init(null, new TrustManager[]{tm}, null);
    
       sslSocketFactory = new TrustAllSSLSocketFactory(ctx);
       if (sslSocketFactory != null)
          sslSocketFactory.setHostnameVerifier(
              SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    
    } catch (Exception ex) {
       Log.e(TAG, ex.getMessage(), ex);
       sslSocketFactory = null;
    }
    
    if (sslSocketFactory == null) {
       sslSocketFactory = SSLSocketFactory.getSocketFactory();
       sslSocketFactory.setHostnameVerifier(
          SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }
    
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    
    // Create an HttpClient with the ThreadSafeClientConnManager.
    // This connection manager must be used if more than one thread will
    // be using the HttpClient.
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    
    DefaultHttpClient client = new DefaultHttpClient(cm, params);
    
    HttpProtocolParams.setUseExpectContinue(client.getParams(), false);
    
    HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
       public boolean retryRequest(IOException exception, int executionCount,
          HttpContext context) {
          // retry a max of 5 times
          if(executionCount >= 5) { return false; }
          if(exception instanceof NoHttpResponseException){
             return true;
          } else if (exception instanceof ClientProtocolException){
             return true;
          }
          return false;
       }
    };
    
    client.setHttpRequestRetryHandler(retryHandler);
    
    /* Cookie Management */
    CookiesStore cookieStore = new BasicCookieStore();
    client.setCookieStore(cookieStore);
    

    在 Volley 中使用 HttpClient

    /* Use HttpClientStack with Volley */
    mRequestQueue = Volley.newRequestQueue(
        context.getApplicationContext(), new HttpClientStack(client));
    

    TrustAllSSLSocketFactory.java

    static final
    private class TrustAllSSLSocketFactory extends SSLSocketFactory {
    
       private SSLContext sslContext = SSLContext.getInstance("TLS");
    
       public TrustAllSSLSocketFactory(KeyStore truststore) 
           throws NoSuchAlgorithmException,
              KeyManagementException,
                  KeyStoreException, UnrecoverableKeyException {
          super(truststore);
    
      TrustManager tm = new X509TrustManager() {
              @Override
              public X509Certificate[] getAcceptedIssuers() { return null; }
    
              @Override
              public void checkServerTrusted(X509Certificate[] chain, String authType)
                 throws CertificateException { }
    
              @Override
              public void checkClientTrusted(X509Certificate[] chain, String authType)
                 throws CertificateException { }
          };
    
          sslContext.init(null, new TrustManager[] { tm }, null);
      }
    
      public TrustAllSSLSocketFactory(SSLContext context)
          throws KeyManagementException, 
                 NoSuchAlgorithmException, KeyStoreException, 
                 UnrecoverableKeyException {
        super(null);
        sslContext = context;
      }
    
      @Override
      public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
         throws IOException, UnknownHostException {
          return sslContext.getSocketFactory()
             .createSocket(socket, host, port, autoClose);  
      }
    
      @Override
      public Socket createSocket() throws IOException {
         return sslContext.getSocketFactory().createSocket();
      }
    };
    

    【讨论】:

    • 我认为这会有所帮助:) 我会在一分钟内对其进行测试。谢谢你的帮助!!!
    猜你喜欢
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2013-07-30
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2015-11-16
    相关资源
    最近更新 更多