SocketClient使用https请求连接


private SocketClient mSocketClient;
private ExecutorService mSocketExecutor;


public void startSocket() {
    //Executors.newSingleThreadExecutor()返回一个线程池(这个线程池只有一个线程)
    // ,这个线程池可以在线程死后(或发生异常时)重新启动一个线程来替代原来的线程继续执行下去!
    mSocketExecutor = Executors.newSingleThreadExecutor(); 
    mSocketExecutor.execute(mSocketTask);
}
 
  
//接收socket通信
 
  
private Runnable mSocketTask = new Runnable() {
    @Override
    public void run() {
      
     getSocketClient().setCallback(new Callback() {
    @Override
    public void onConnected() {
        
    }

    @Override
    public void onConnectError(String msg) {

    }

    @Override
    public void onKicked() {

    }

    @Override
    public void onReceived(int type, String jsonStr, byte[] bin) {

    }

    @Override
    public void onSendOverTime(int type, String id, String json) {

    }
});
}};
 
  
 
  
 
  
//创建 mSocketClient 

private SocketClient getSocketClient() throws Exception {
    if (isTLS) {
        LogUtils.e(TAG, "连接TLS网络", null);
        //取得SSLSSLContext实力+
        SSLContext sslContext = SSLContext.getInstance("TLS");
        //取得KeyManagerFactoryTrustManagerFactoryX509秘钥管理器实例
        KeyStore tks = KeyStore.getInstance("BKS");
        tks.load(getResources().openRawResource(R.raw.client),//证书
                "123456".toCharArray());

        EasyX509TrushManager trushManager = new EasyX509TrushManager(tks);

        sslContext.init(null, new TrustManager[]{trushManager}, null);
        SocketFactory factory = sslContext.getSocketFactory();
        return mSocketClient = SocketClient.getDefault(ClientConfig.getDefault("app.xxxx.cn", 9100, factory));
    } else {
        LogUtils.e(TAG, "连接普通网络", null);
        return mSocketClient = SocketClient.getDefault(ClientConfig.getDefault("xxx.xx.xxx.xx", 9002));
    }
}
 
  
 
  
public class EasyX509TrushManager implements X509TrustManager {

    private X509TrustManager standardTrustManager = null;

    /**
     * Constructor for EasyX509TrustManager.
     */
    public EasyX509TrushManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
        super();
        TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        factory.init(keystore);
        TrustManager[] trustmanagers = factory.getTrustManagers();
        if (trustmanagers.length == 0) {
            throw new NoSuchAlgorithmException("no trust manager found");
        }
        this.standardTrustManager = (X509TrustManager) trustmanagers[0];
    }

    /**
     * @see X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
     */
    public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
        standardTrustManager.checkClientTrusted(certificates, authType);
    }

    /**
     * @see X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
     */
    public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
        if ((certificates != null) && (certificates.length == 1)) {
            certificates[0].checkValidity();
        } else {
            standardTrustManager.checkServerTrusted(certificates, authType);
        }
    }

    /**
     * @see X509TrustManager#getAcceptedIssuers()
     */
    public X509Certificate[] getAcceptedIssuers() {
        return this.standardTrustManager.getAcceptedIssuers();
    }


}

 
  

你可能感兴趣的:(SocketClient使用https请求连接)