Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中使用wasync的WebSocket连接不起作用_Android_Websocket_Atmosphere - Fatal编程技术网

在Android中使用wasync的WebSocket连接不起作用

在Android中使用wasync的WebSocket连接不起作用,android,websocket,atmosphere,Android,Websocket,Atmosphere,我在Android应用程序中使用WebSocket。 我在陈述如下活动时出错 在使用Chrome for Android测试安全websocket时,这两种情况都可以正常工作, 都不是uri(“https://echo.websocket.org”或uri(“https://”工作正常 我的代码有什么问题 我的设备是Nexus7(2014),Android 4.4.2 private Socket socket; @Override protected void

我在Android应用程序中使用WebSocket。 我在陈述如下活动时出错

在使用Chrome for Android测试安全websocket时,这两种情况都可以正常工作, 都不是
uri(“https://echo.websocket.org”
uri(“https://”
工作正常

我的代码有什么问题

我的设备是Nexus7(2014),Android 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();
    }

我的应用程序有权限android.permission.INTERNET、android.permission.ACCESS\u NETWORK\u STATE、android.permission.ACCESS\u WIFI\u STATE。

试试这个方法,它对我有用。 我建议在后台使用。 下面是android活动中大气的代码

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.
                            */
                        }
                    });
                }
            });
AtmosphereClient client=ClientFactory.getDefault().newClient(AtmosphereClient.class);
final RequestBuilder request=client.newRequestBuilder()
.method(Request.method.GET)
.uri(atmosphereServletPath)
.trackMessageLength(真)
/*
*用于将默认传输设置为Websocket的代码
*/
.transport(Request.transport.WEBSOCKET)
/*
*将回退传输设置为长轮询的代码
*/
.transport(请求、传输、长轮询);
/*
*使用创建的客户端在服务器端创建套接字的代码
*/
socket=client.create();
/*
*打开套接字的代码
*/
socket.open(request.build());
/*
*消息处理程序的代码
*/
socket.on(“消息”,新函数(){
@凌驾
上的公共无效(最终对象){
uiHandler.post(新的Runnable(){
/*
*@see java.lang.Runnable#run()这里我收到了
*从服务器删除您的对象
*/
@凌驾
公开募捐{
/*
*onMessage()方法的任务。
*/
}
});
}
});

这里的
atmosphereServletPath
是您的Atmosphereservlet的路径。

之前打开
套接字,将
的第一个参数链接到
字符串,或者添加
传输(Request.transport.LONG_POLLING)
对我来说都不起作用:-(请附上Atmosphere Servlet的服务器端代码。如果我以桌面应用程序的形式运行上述代码,它可以正常工作。因此,我不认为这是服务器端问题。
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.
                            */
                        }
                    });
                }
            });