I'm writing a android chat application with socket.io-client-java.I want to 
check whether the client user exist at first.So I need to send a command 
like "user/exist" to server url and get the response from server.I need to 
wait the server response then can go to next step.But the socket.io use the 
asynchronous callback.For getting the response synchronous I known the 
Furture and Callable only.So I tried the way using code as below:But it 
dose not work.The respObj is null. How can i get the reponse synchronous? I 
am a green hand on java and forgive my poor chinese english. Any help would 
be appreciated!

//this is request method using socket.iopublic JSONObject request(final String 
method,final String url,final JSONObject data){
    final JSONObject responseObj = new JSONObject();
    if (mSocket.connected()) {
             mSocket.emit(method, reqObj, new Ack() {
                @Override
                public void call(Object... objects) {
                    System.out.println("get Ack");
                    try {
                        responseObj.put("body", (JSONObject) objects[0]);
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            })
         }}//this is Callable call implement
 @Override
    public JSONObject call(){
      return request("get","https://my-chat-server/user/exist",new 
JSONObject());}
//this is call method in activity
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<JSONObject> response = executor.submit(mApiSocket);
        executor.shutdown();
        JSONObject respObj = new JSONObject();
        JSONObject respBody = new JSONObject();
        try {
            respObj = response.get();
            respBody = respObj.getJSONObject("body");
        }catch (ExecutionException e){

        }catch(InterruptedException e1){

        }catch(JSONException e2){

       }

And I known the js can use Promise and await like below:

//request methodstatic request(method, url, data) {

    return new Promise((resolve, reject) => {

        this.socket.emit(method,
            {
                url: url, 
                method,
                data, 
            },
            async (res) => {
                if (res.statusCode == 100) { 
                    resolve(res.body, res); 
                } else {
                    throw new Error(`${res.statusCode} error: ${res.body}`);
                    reject(res.body, res);
                }
            }
        )

    })
}//call method
response = await mSocket.request('get','https://my-chat-server/user/exist', {
            first_name: 'xu',
            last_name: 'zhitong',

        });

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/0593dce8-483a-49ba-90b7-96cc0c2b8acd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to