參考鏈接:http://www.juliankrone.com/connect-and-transfer-data-with-secure-websockets-in-android/
WebSocket正常連接http開頭的鏈接都沒什么問題,但是連接wss開頭的鏈接IOS沒有問題,安卓這邊就連接不上了。通過查找資料發現需要對WebSocketClient設置setWebSocketFactory()這個方法。在new完WebSocketClient后面加入以下代碼再進行連接就可以了。
// WebSocket連接wss鏈接 // This part is needed in case you are going to use self-signed // certificates TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[] {}; } public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } } }; SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); // Otherwise the line below is all that is needed. // sc.init(null, null, null); wsc.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));這里需要用WebSocket的1.3.0的jar包,下載鏈接:http://grepcode.com/snapshot/repo1.maven.org/maven2/org.java-websocket/Java-WebSocket/1.3.0/AS的話直接:
compile'org.java-websocket:Java-WebSocket:1.3.0'權當自己做個記錄啦~
新聞熱點
疑難解答