我使用spring websocket编写websocket客户端:
WebSocketClient webSocketClient =
new StandardWebSocketClient();
List<Transport> transports = new ArrayList<>(
Collections.singletonList(new WebSocketTransport(webSocketClient))
);
SockJsClient sockJsClient = new SockJsClient(transports);
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
String url = "ws://localhost:8080/chat-endpoint";
StompSessionHandler sessionHandler = new StompSessionChatHandler();
StompSession session = stompClient.connect(url, sessionHandler).get();
它在桌面java应用程序上工作。
我的目标是在android上创建一个websocket客户端,所以我尝试在android客户端上移植以下代码:
public class Client {
private StompSessionHandler sessionHandler = null;
private StompSession session = null;
private StandardWebSocketClient standardWebSocketClient = null;
private SockJsClient sockJsClient = null;
private WebSocketStompClient stompClient = null;
private static class StompSessionChatHandler extends StompSessionHandlerAdapter {
}
public Client() throws ExecutionException, InterruptedException {
standardWebSocketClient =
new StandardWebSocketClient();
List<Transport> transports =
new ArrayList<>(Collections.singletonList(new WebSocketTransport(standardWebSocketClient)));
sockJsClient = new SockJsClient(transports);
stompClient =
new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
String urlEndpoint = "ws://10.0.2.2:8080/chat-endpoint";
this.sessionHandler = new StompSessionChatHandler();
this.session = stompClient.connect(urlEndpoint, sessionHandler).get();
}
}
我从spring内部得到一个错误,比如服务器url不正确;尽管桌面java应用程序上的相同url被认为是正确的:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.hse.guidehelper, PID: 7048
java.lang.ExceptionInInitializerError
at org.springframework.web.util.UriComponentsBuilder.fromUri(UriComponentsBuilder.java:208)
at org.springframework.web.socket.sockjs.client.SockJsUrlInfo.getInfoUrl(SockJsUrlInfo.java:85)
at org.springframework.web.socket.sockjs.client.SockJsClient.getServerInfo(SockJsClient.java:290)
at org.springframework.web.socket.sockjs.client.SockJsClient.doHandshake(SockJsClient.java:260)
at org.springframework.web.socket.messaging.WebSocketStompClient.connect(WebSocketStompClient.java:270)
at ru.hse.guidehelper.chat.Client.<init>(Client.java:103)
at ru.hse.guidehelper.chat.DialogsActivity.addAllChatsInAdapter(DialogsActivity.java:71)
at ru.hse.guidehelper.chat.DialogsActivity.onCreate(DialogsActivity.java:60)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 96
^(([^:/?#]+):)?(//(([^@\[/?#]*)@)?(\[[\p{XDigit}:.]*[%\p{Alnum}]*]|[^\[/?#:]*)(:(\d*(?:\{[^/]+?})?))?)?([^?#]*)(\?([^#]*))?(#(.*))?
^
at com.android.icu.util.regex.PatternNative.compileImpl(Native Method)
at com.android.icu.util.regex.PatternNative.<init>(PatternNative.java:39)
at com.android.icu.util.regex.PatternNative.create(PatternNative.java:35)
at java.util.regex.Pattern.compile(Pattern.java:1426)
at java.util.regex.Pattern.<init>(Pattern.java:1401)
at java.util.regex.Pattern.compile(Pattern.java:959)
at org.springframework.web.util.UriComponentsBuilder.<clinit>(UriComponentsBuilder.java:97)
... 23 more
有什么问题吗?先谢谢你。
p、 美国偶数 ws://localhost:8080/chat-endpoint
在android客户端上抛出此错误
暂无答案!
目前还没有任何答案,快来回答吧!