2011-08-23 41 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在这两个包中都有。