简体   繁体   中英

Boolean Return from AsyncTask in Android

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM