我在Android应用程序中使用wasync来使用WebSocket。我在陈述以下活动时出错了。
在测试安全websocket时http://www.websocket.org/echo.htmlAndroid版Chrome在这两种情况下都很好,CCD_ 1和CCD_。
我的代码出了什么问题?
我的设备是Nexus7(2014(,安卓4.4.2。
private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wasync_test);
new Thread() {
public void run() {
AtmosphereClient client = ClientFactory.getDefault().newClient(AtmosphereClient.class);
AtmosphereRequest.AtmosphereRequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.enableProtocol(false)
.uri("https://echo.websocket.org")
.transport(Request.TRANSPORT.WEBSOCKET);
socket = client.create()
// on(MESSAGE, OPEN, CLOSE) are omitted
.on(Event.ERROR, new Function<Object>() {
@Override
public void on(Object o) {
Log.e(TAG, "[wasync] onerror: " + o);
}
});
try {
socket.open(request.build());
} catch (IOException e) {
}
}
}.start();
}
我得到的错误是:
05-01 18:33:32.050 20891-20904/myapp E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-267
Process: myapp, PID: 20891
java.lang.RuntimeException: java.net.ConnectException: wss://echo.websocket.org/?X-Atmosphere-Transport=websocket
at myapp.WasyncTestActivity.run(WasyncTestActivity.java:84)
at myapp.WasyncTestActivity.access$000(WasyncTestActivity.java:30)
at myapp.WasyncTestActivity$1.run(WasyncTestActivity.java:41)
Caused by: java.net.ConnectException: wss://echo.websocket.org/?X-Atmosphere-Transport=websocket
at com.ning.http.client.providers.netty.NettyConnectListener.operationComplete(NettyConnectListener.java:103)
at org.jboss.netty.channel.DefaultChannelFuture.notifyListener(DefaultChannelFuture.java:427)
at org.jboss.netty.channel.DefaultChannelFuture.notifyListeners(DefaultChannelFuture.java:413)
at org.jboss.netty.channel.DefaultChannelFuture.setFailure(DefaultChannelFuture.java:380)
at org.jboss.netty.handler.ssl.SslHandler.channelDisconnected(SslHandler.java:661)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:102)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
at org.jboss.netty.channel.Channels.fireChannelDisconnected(Channels.java:396)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.close(AbstractNioWorker.java:360)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:93)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)
at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.nio.channels.ClosedChannelException
at org.jboss.netty.handler.ssl.SslHandler.channelDisconnected(SslHandler.java:661)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:102)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
at org.jboss.netty.channel.Channels.fireChannelDisconnected(Channels.java:396)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.close(AbstractNioWorker.java:360)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:93)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)
at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
05-01 18:33:32.070 653-664/? W/ActivityManager﹕ Force finishing activity myapp/.WasyncTestActivity
05-01 18:33:32.150 20891-20911/myapp E/WaitingActivity﹕ [wasync] onerror: java.net.ConnectException: wss://echo.websocket.org/?X-Atmosphere-Transport=websocket
我的应用程序具有android.permission.INTERNET、android.pPermission.ACCESS_NETWORK_STATE、android.permission.ACCESS_WIFI_STATE.权限
试试这种方法,它对我有效。我建议使用AsyncTask在后台执行与氛围相关的任务。这是安卓活动中大气的代码
AtmosphereClient client = ClientFactory.getDefault().newClient(AtmosphereClient.class);
final RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri(atmosphereServletPath)
.trackMessageLength(true)
/*
* code to set the default transport to Websocket
*/
.transport(Request.TRANSPORT.WEBSOCKET)
/*
* code to set the fall-back transport to Long-Polling
*/
.transport(Request.TRANSPORT.LONG_POLLING);
/*
* code to create the socket at server side using the created client
*/
socket = client.create();
/*
* code to open the socket
*/
socket.open(request.build());
/*
* code for Message handler
*/
socket.on("message", new Function<YourObject>() {
@Override
public void on(final YourObject s) {
uiHandler.post(new Runnable() {
/*
* @see java.lang.Runnable#run() Here I've received the
* YourObject from the server
*/
@Override
public void run() {
/*
* tasks for onMessage() method.
*/
}
});
}
});
这里的atmosphereServletPath
是指向您的氛围servlet的路径。