如何使用 OKHTTP3 修复 java.lang.IllegalArgumentException:意外 url
How to fix java.lang.IllegalArgumentException: unexpected url using OKHTTP3
当我尝试在 Okhttp 中为 Websocket 执行以下代码时,出现错误消息:
原因:java.lang.IllegalArgumentException:意外 url:wws://www.mydomain。com/websocket
String WEBSOCKET_URL = "wws://www.mydomain.com/websocket";
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder().url(WEBSOCKET_URL).build();
WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, okhttp3.Response response) {
Toast.makeText(HomeCoach.this, "Connected to the Websocket server", Toast.LENGTH_SHORT).show();
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, @Nullable okhttp3.Response response) {
Toast.makeText(HomeCoach.this, "Error ", Toast.LENGTH_SHORT).show();
}
});
不是WWS,是WSS。将 url 更改为 String WEBSOCKET_URL = "wss://www.mydomain.com/websocket";
当我尝试在 Okhttp 中为 Websocket 执行以下代码时,出现错误消息:
原因:java.lang.IllegalArgumentException:意外 url:wws://www.mydomain。com/websocket
String WEBSOCKET_URL = "wws://www.mydomain.com/websocket";
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder().url(WEBSOCKET_URL).build();
WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, okhttp3.Response response) {
Toast.makeText(HomeCoach.this, "Connected to the Websocket server", Toast.LENGTH_SHORT).show();
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, @Nullable okhttp3.Response response) {
Toast.makeText(HomeCoach.this, "Error ", Toast.LENGTH_SHORT).show();
}
});
不是WWS,是WSS。将 url 更改为 String WEBSOCKET_URL = "wss://www.mydomain.com/websocket";