我有一个问题,任何帮助都很感激。我的测试设备是华硕Nexus 7安卓5.1。我想使用Jetty Websockets连接到web服务器。url使用wss,我在manifest.xml文件中编辑了互联网权限。下面是代码:
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* Created by Christian on 10.07.2015.
*/
public class Socket extends WebSocketAdapter {
private static final Logger LOG = Log.getLogger(Socket.class);
@Override
public void onWebSocketConnect(Session session) {
super.onWebSocketConnect(session);
LOG.info("onConnect({})", session);
}
@Override
public void onWebSocketText(String message) {
super.onWebSocketText(message);
LOG.info("onMessage() - {}", message);
}
@Override
public void onWebSocketClose(int statusCode, String reason) {
super.onWebSocketClose(statusCode, reason);
LOG.info("onClose({}, {})", statusCode, reason);
}
@Override
public void onWebSocketError(Throwable cause) {
super.onWebSocketError(cause);
LOG.warn(cause);
}
}
这是websocket类。我在我的android应用程序的MainActivity中启动websocket客户端:
URI uri = URI.create("wss://localhost:8600");
SslContextFactory ssl = new SslContextFactory();
ssl.setTrustAll(true);
WebSocketClient client = new WebSocketClient(ssl);
try {
client.start();
Socket socket = new Socket();
ClientUpgradeRequest request = new ClientUpgradeRequest();
Future<Session> fut = client.connect(socket, uri);
} catch (Throwable t) {
LOG.warn(t);
}
编译后,日志显示以下错误,由此我假设服务器无法连接到服务器。
07-11 16:23:50.175 10167-10167/programmieren2.simplechat W/System.err﹕ Defaulting Uptime to NOIMPL due to (java.lang.UnsupportedOperationException) Implementation not available in this environment
07-11 16:23:50.201 10167-10167/programmieren2.simplechat W/System.err﹕ 2015-07-11 16:23:50.200:INFO::main: Logging initialized @-1ms
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ 2015-07-11 16:23:50.450:WARN:oejuc.AbstractLifeCycle:main: FAILED SslContextFactory@da9981a(null,null): java.lang.NoSuchMethodError: No virtual method setEndpointIdentificationAlgorithm(Ljava/lang/String;)V in class Ljavax/net/ssl/SSLParameters; or its super classes (declaration of 'javax.net.ssl.SSLParameters' appears in /system/framework/core-libart.jar)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ java.lang.NoSuchMethodError: No virtual method setEndpointIdentificationAlgorithm(Ljava/lang/String;)V in class Ljavax/net/ssl/SSLParameters; or its super classes (declaration of 'javax.net.ssl.SSLParameters' appears in /system/framework/core-libart.jar)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.customize(SslContextFactory.java:1382)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.newSSLEngine(SslContextFactory.java:1328)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:309)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.websocket.client.WebSocketClient.doStart(WebSocketClient.java:256)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at com.example.khodor.simplechatneuerversuch.MainActivity.onCreate(MainActivity.java:62)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5990)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-11 16:23:50.451 10167-10167/programmieren2.simplechat W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ 2015-07-11 16:23:50.452:WARN:oejuc.AbstractLifeCycle:main: FAILED org.eclipse.jetty.websocket.client.WebSocketClient@25ca86c5: java.lang.NoSuchMethodError: No virtual method setEndpointIdentificationAlgorithm(Ljava/lang/String;)V in class Ljavax/net/ssl/SSLParameters; or its super classes (declaration of 'javax.net.ssl.SSLParameters' appears in /system/framework/core-libart.jar)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ java.lang.NoSuchMethodError: No virtual method setEndpointIdentificationAlgorithm(Ljava/lang/String;)V in class Ljavax/net/ssl/SSLParameters; or its super classes (declaration of 'javax.net.ssl.SSLParameters' appears in /system/framework/core-libart.jar)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.customize(SslContextFactory.java:1382)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.newSSLEngine(SslContextFactory.java:1328)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:309)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.websocket.client.WebSocketClient.doStart(WebSocketClient.java:256)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at com.example.khodor.simplechatneuerversuch.MainActivity.onCreate(MainActivity.java:62)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5990)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-11 16:23:50.452 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ 2015-07-11 16:23:50.453:WARN:ceks.MainActivity:main:
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ java.lang.NoSuchMethodError: No virtual method setEndpointIdentificationAlgorithm(Ljava/lang/String;)V in class Ljavax/net/ssl/SSLParameters; or its super classes (declaration of 'javax.net.ssl.SSLParameters' appears in /system/framework/core-libart.jar)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.customize(SslContextFactory.java:1382)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.newSSLEngine(SslContextFactory.java:1328)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:309)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.websocket.client.WebSocketClient.doStart(WebSocketClient.java:256)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at com.example.khodor.simplechatneuerversuch.MainActivity.onCreate(MainActivity.java:62)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5990)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-11 16:23:50.453 10167-10167/programmieren2.simplechat W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
同样的问题。问题似乎是在Android中重新实现SSLParameters,您可以在Android .jar中找到。这个实现甚至有一个名为"endpointIdentificationAlgorithm"的字段,但没有使用,所以我想它将在未来实现。
我做了一个解决方案,通过覆盖"定制"-方法调用丢失的setEndpointIdentificationAlgorithm和删除的行,其余的是原始代码:
this.client = new WebSocketClient(new SslContextFactory(true){
@Override
public void customize(SSLEngine sslEngine) {
SSLParameters sslParams = sslEngine.getSSLParameters();
//sslParams.setEndpointIdentificationAlgorithm(_endpointIdentificationAlgorithm);
sslEngine.setSSLParameters(sslParams);
if (getWantClientAuth())
sslEngine.setWantClientAuth(getWantClientAuth());
if (getNeedClientAuth())
sslEngine.setNeedClientAuth(getNeedClientAuth());
sslEngine.setEnabledCipherSuites(selectCipherSuites(
sslEngine.getEnabledCipherSuites(),
sslEngine.getSupportedCipherSuites()));
sslEngine.setEnabledProtocols(selectProtocols(sslEngine.getEnabledProtocols(),sslEngine.getSupportedProtocols()));
}
});