0

My Activity code:

public String getValue(JSONObject data) {
    String value;
    socket.emit("request", data, new Ack() {
        @Override
        public void call(Object... args) {
            value = (String) args[1]; // how to wait for this value?
        });

    return value;
}

How do I wait for the value that came from the socket.io callback?

Footniko
  • 2,394
  • 2
  • 25
  • 34

1 Answers1

0

Try this

public class  getvalue extends AsyncTask<JSONObject,Void,String>{


          @Override
          protected String doInBackground(JSONObject... jsonObjects) {
              String value;
              socket.emit("request", jsonObjects, new Ack() {
                  @Override
                  public void call(Object... args) {
                      value = (String) args[1]; // how to wait for this value?
                  }
          });
          return value;
      }
          protected void onPostExecute(String result) {
              //result is the final String;
          }
      }

And after that go to Activity that you want to use this function in and try this :

 getvalue a=new getvalue();
            getvalue.execute(data);

Here is some useful link https://developer.android.com/reference/android/os/AsyncTask

AsyncTask Android example hopefully to work

amrtaweel
  • 74
  • 4
  • 1
    thanks for the help but as you can see I need to wait for the socket emit callback which is asynchronous. So, it's not gonna work. – Footniko May 20 '20 at 20:51