0

I want to return a Boolean after a AsyncTask.

This is the AsyncTask (not the whole code because isn't important and sstackoverflow give me error):

public class CallSoap extends AsyncTask<CallSoapParams, Void, Void> {

public interface AsyncResponse {
    void processFinish(String output);
}

private Context activityContext;

public AsyncResponse delegate = null;//Call back interface

public CallSoap(Context context, AsyncResponse asyncResponse) {
    activityContext = context;
    delegate = asyncResponse;
}

@Override
protected Void doInBackground(CallSoapParams... params) {

    request = new SoapObject(params[0].NAMESPACE, params[0].METHOD_NAME);
    // no important things
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //dismiss ProgressDialog
    delegate.processFinish(response.toString());
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //create and show ProgressDialog
}
}

And this is the implementation on Activity (not the whole code because isn't important and sstackoverflow give me error):

    private boolean checkDataRegistrationByServer() {
    if (NickNameExist()) {
        // DO STUFF
    }
    return true;
}

Boolean r;
private boolean NickNameExist() {
    CallSoapParams callParams = new CallSoapParams(NICKNAME_EXIST);

    CallSoap NickNameExistCall = new CallSoap(RegistrationActivity.this, new CallSoap.AsyncResponse() {

        @Override
        public void processFinish(String output) {
            Log.d("Response From AsyTask:", output);
            if (output.equals(FALSE_RESPONSE)) {
                r = false;
                Toast.makeText(getApplicationContext(), output + " - NickNameExistCall - Nick don't exist", Toast.LENGTH_SHORT).show();
            } else {
                r = true;
            }
        }
    });
    NickNameExistCall.execute(callParams);

    return r;
}

I tried to create a global Boolean but the App crash. Someone can help me?

Damix
  • 3
  • 2

1 Answers1

0

1) You don't have a response variable anywhere, and doInBackground has returned null instead of any response, so not clear how you got that value.

 delegate.processFinish(response.toString());

2) You can't return from that function. And your app crashes probably because Boolean's can be null. boolean's cannot. However, you should not attempt to make a global variable here because that's not how asynchronous code should run.

What you need is to pass the callback through the function

private void checkDataRegistrationByServer(String data, CallSoap.AsyncResponse callback) { 
    CallSoap nickNameExistCall = new CallSoap(RegistrationActivity.this, callback);
    CallSoapParams callParams = new CallSoapParams(data);
    nickNameExistCall.execute(callParams);
}

Elsewhere...

final String nick = NICKNAME_EXIST;
checkDataRegistrationByServer(nick, new CallSoap.AsyncResponse() {

    @Override
    public void processFinish(String response) {
        Log.d("Response From AsyncTask:", output);
        boolean exists = !response.equals(FALSE_RESPONSE);
        if (!exists) {
            Toast.makeText(getApplicationContext(), output + " - NickNameExistCall - Nick " + nick + " doesn't exist", Toast.LENGTH_SHORT).show();
        }
    }
});

Note: If you make your AsyncTask just return a Boolean in the AsyncResponse you can shorten this code some.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you. I try to use a different way: set doInBackground String and used onPostExecute. Is this way the same? – Damix Feb 25 '18 at 22:26
  • Not sure what you mean by that – OneCricketeer Feb 25 '18 at 22:28
  • I found this: https://stackoverflow.com/questions/10972114/how-to-get-a-string-back-from-asynctask – Damix Feb 25 '18 at 22:34
  • No, no. Never use `.get()`. That defeats the whole purpose of **async** tasks – OneCricketeer Feb 25 '18 at 22:36
  • Ah, thanks. However I don't understand how to use properly your code. – Damix Feb 25 '18 at 22:42
  • All I did was take your code for `checkDataRegistrationByServer()` and added 2 parameters to it... Not that hard to figure out, is it? You cannot use `NickNameExist()` and have it return a boolean because the internal function itself is asynchronous. By *waiting for a return result*, you are thinking in synchronous code again. – OneCricketeer Feb 25 '18 at 22:55
  • Thank you, tomorrow (I'm Italian) I will try to implement your code. – Damix Feb 25 '18 at 23:01