我的活动代码:
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;
}
我如何等待来自socket.io回调的value
?
尝试此
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;
}
}
然后在“活动”中您要使用此功能的地方尝试此:
getvalue a=new getvalue();
getvalue.execute(data);
这里有一些有用的链接https://developer.android.com/reference/android/os/AsyncTask