I have a problem with returning the result value to onPostExecute()
of an onBackground()
function in the Async Class of Android.
What I want to do is to search for a specified contact name in a database using socket.io and then when I find it; return
true
to onPostExecute
so that I can do something there when the contact has been found. If nothing has been found, false
shall be returned and something else should be done in onPostExecute()
.
I set the boolean
global value "success" to true
or false
respectively and try to pass this value to onPostExecute()
, but for some reason the value is assigned AFTER onPostExecute()
is called ... must have something to do with asynchronous operation, but since I am not an expert, I hope someone can give me some hint on how to do it right so that the boolean
value is updated, BEFORE onPostExecute()
checks the value.
This is my code:
public class AsynchronicTask extends AsyncTask<Object, Void, Boolean> {
SocketIO socket = null;
AsyncObject asyncObject = null;
Boolean success = false;
@Override
protected Boolean doInBackground(final Object... argument) {
this.asyncObject = (AsyncObject) argument[0];
try {
System.out.println("Trying to connect to: "
+ asyncObject.getURL());
socket = new SocketIO(asyncObject.getURL());
socket.connect(new IOCallback() {
@Override
public void onConnect() {
JSONObject queriedUsername = new JSONObject();
try {
queriedUsername.put("Username", asyncObject.getQueryInput());
} catch (JSONException e) {
e.printStackTrace();
}
socket.emit("clientRequestSearchUsername", queriedUsername);
}
@Override
public void onDisconnect() {
}
@Override
public void onError(SocketIOException arg0) {
}
@Override
public void onMessage(String arg0, IOAcknowledge arg1) {
}
@Override
public void onMessage(JSONObject arg0, IOAcknowledge arg1) {
}
@Override
public void on(final String message, IOAcknowledge argIO,
final Object ... arg) {
if (message.equals(asyncObject.getServerMessage())) {
if (arg[0].equals("null")) {
Log.e("ERROR", "No Result Found");
success = false;
} else {
success = true;
Log.d("DEBUG", "RESULT OK!");
}
};
}
});
} catch (MalformedURLException e) {
Log.e("error", "Wrong Server URL");
e.printStackTrace();
}
return success;
}
@Override
protected void onPostExecute(Boolean result) {
Log.d("DEBUG", result.toString());
// Give reply when username query has finished
if (result.equals(true)){
Log.d("DEBUG", "Contact found, adding to contactlist");
// Populate data in contactViewAdapter
asyncObject.getContactListAdapter().populateAdapter(asyncObject.getQueryInput());
Toast.makeText(asyncObject.getContext(), "Contact added", Toast.LENGTH_SHORT).show();
}
else {
Log.d("DEBUG", "No Success adding contact, no contact found");
Toast.makeText(asyncObject.getContext(), "Username not existent, no contact added", Toast.LENGTH_SHORT).show();
}
}
}