Pushbullet 这个很棒的服务有一个 websocket 流,你可以订阅它,然后监听到你设备的推送,这正是我想要的。我希望我的应用程序能够连接到消息流并根据他们所说的做一些事情。
我试过使用https://github.com/andrepew/Java-WebSocket/tree/1.3.0-Android-SSL-Fix(来自https://github.com/TooTallNate/Java-WebSocket),但我没有运气。超时后,连接响应返回
onClose -1, draft org.java_websocket.drafts.Draft_10@a38fd36 refuses handshake, false
和
05-22 03:24:30.709 15423-15904 java.lang.NullPointerException: ssl == null
05-22 03:24:30.710 15423-15904 com.android.org.conscrypt.NativeCrypto.SSL_read_BIO(Native Method)
05-22 03:24:30.710 15423-15904 com.android.org.conscrypt.OpenSSLEngineImpl.unwrap(OpenSSLEngineImpl.java:477)
05-22 03:24:30.710 15423-15904 javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:1006)
我的代码是(没有我的访问令牌......)甚至尝试“信任所有主机”建议的黑客,
private void createConnection()
{
URI uri = URI.create("wss://stream.pushbullet.com/websocket/" + pushAT);
final WebSocketClient mWebSocketClient = new WebSocketClient(uri) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
Log.d(TAG, "onOpen " + serverHandshake);
}
@Override
public void onMessage(String s) {
Log.d(TAG, "onMessage " + s);
}
@Override
public void onClose(int i, String s, boolean b) {
Log.d(TAG, "onClose " + i + ", " + s + ", " + b);
}
@Override
public void onError(Exception e) {
Log.d(TAG, "onError " + e);
e.printStackTrace();
}
};
Log.d(TAG, "Connecting to " + uri);
trustAllHosts(mWebSocketClient);
mWebSocketClient.connect();
Log.d(TAG, "Connecting to " + uri);
}
public void trustAllHosts(WebSocketClient wsc) {
// Create a trust manager that does not validate certificate chains
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 {
}
}};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
wsc.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));
} catch (Exception e) {
e.printStackTrace();
}
}