2011-08-23 23 views
1

我已經創建了自己的SSLSocketFactory在this question面臨類轉換誤差兩個包

private SSLSocketFactory newSslSocketFactory() { 
    try { 
     // Get an instance of the Bouncy Castle KeyStore format 
     KeyStore trusted = KeyStore.getInstance("BKS"); 
     // Get theraw resource, which contains the keystore with 
     // your trusted certificates (root and any intermediate certs) 
     Context context = this.activity; 
     Resources _resources = context.getResources(); 
     InputStream in = _resources.openRawResource(R.raw.mystore); 
     try { 
      // Initialize the keystore with the provided trusted certificates 
      // Also provide the password of the keystore 
      trusted.load(in, "mypassword".toCharArray()); 
     } finally { 
      in.close(); 
     } 
     // Pass the keystore to the SSLSocketFactory. The factory is responsible 
     // for the verification of the server certificate. 
     SSLSocketFactory sf = new SSLSocketFactory(trusted); 
     // Hostname verification from certificate 

     sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); 
     return sf; 
    } catch (Exception e) { 
     throw new AssertionError(e); 
    } 
} 

解釋之間其實我需要在連接之前設置此SSLSocketFactoryHttpsURLConnection。但是當我試圖通過調用

(HttpsURLConnection)connection.setSSLSocketFactory(trusted); 

在這一點上,以將其放置在HttpsURLConnection現在面臨2包org.apache.http.conn.ssl.SSLSocketFactoryjavax.net.ssl.SSLSocketFactory之間投類錯誤。

如何解決這個問題?

+0

請發佈異常的完整堆棧跟蹤。 –

回答

0

上面這段代碼沒有得到任何異常。

但問題是,當我試圖在HttpsURLConnectionSSLSocketFactory使用設置:

(HttpsURLConnection)connection.setSSLSocketFactory(trusted) 

是表示「在類型HttpsURLConnection方法setSSLSocketFactory(SSLSocketFactory)不適用於參數(SSLSocketFactory)」 。

因爲方法setSSLSocketFactory在這兩個包中都有。