gpt4 book ai didi

java - https ://websocket not connect android

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:24 25 4
gpt4 key购买 nike

我有连接套接字 http://(with port) url 是完整的并且可以工作但是我有连接 https://(without port) 在 Java 中不工作-WebSocket。

我使用 gradle 依赖 'org.java-websocket:Java-WebSocket:1.3.0'

WebSocketClient   mWebSocketClient = new WebSocketClient(uri) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
Log.e(TAG, "Websocket: " + "Opened");
mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
}

@Override
public void connect() {
super.connect();
//Log.e(TAG, "Websocket: " + "connect");
SharedPreference.storeValue(mContext, SharedPreference.WEB_SOCKET_STATUS, SharedPreference.WEB_SOCKET_CONNECT);
}
}

最佳答案

我找到了解决方案!)如果你想使用 wss:// 你应该设置 setWebSocketFactory.. 就像这样:

public void connect() {

final String url = "wss://echo.websocket.org";
URI uri=null;
try{
uri = new URI(url);
}
catch (URISyntaxException e)
{
Log.e(TAG, e.getMessage());
return;
}
mWebSocketClient = new WebSocketClient(uri) {
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.e(TAG,"Connected!");
}

@Override
public void onMessage(String message) {
Log.e(TAG,"Message: "+message);

}

@Override
public void onClose(int code, String reason, boolean remote) {
Log.e(TAG,"Disconnected!");

}

@Override
public void onError(Exception ex) {
Log.e(TAG,"Error: "+ex.getMessage());

}
};

if (url.indexOf("wss") == 0) {
try {
SSLContext sslContext = SSLContext.getDefault();
mWebSocketClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
} catch (Exception e) {
e.printStackTrace();
}
}

mWebSocketClient.connect();
}

关于java - https ://websocket not connect android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39718846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com