0

I've looked at so many questions and answers but none of what i found has actually worked!

So basically if the title doesn't help much then what I'm trying to do is execute an AsyncTask from a dialog but it's not executing and when it does, it'll show up as an null object and if I'm honest it's bloody annoying!

So if anyone can help then that would be great.

The Class is subbed.

Here's the Async class:

static class UpdatePassword extends AsyncTask<String, String, String> {

    Context context;
    String oldPassword;
    String newPassword;

    public UpdatePassword(String setOldPassword, String setNewPassword, Context context) {
        this.oldPassword = setOldPassword;
        this.newPassword = setNewPassword;
        this.context = context;
    }

    @Override protected String doInBackground(String... params) {
        HttpRequestUtils httpRequestUtils = new HttpRequestUtils(context);
        if (TextUtils.isEmpty(oldPassword) || TextUtils.isEmpty(newPassword)) {
            return null;
        } else {
            String response = null;
            String baseUrl = "rest/ws/user/update/password";
            ApiResponse apiResponse = null;
            try {
                response = httpRequestUtils.getResponse(baseUrl + "?oldPassword=" + oldPassword + "&newPassword=" + newPassword, "application/json", "application/json");
                if (TextUtils.isEmpty(response)) {
                    return null;
                }
                apiResponse = (ApiResponse) GsonUtils.getObjectFromJson(response, ApiResponse.class);
                if (apiResponse != null && apiResponse.isSuccess()) {
                    return apiResponse.getStatus();
                }
                Log.i("Update", "password call" + apiResponse);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return newPassword;
        }
    }
}

And here's what I'm doing to execute it:

String oldPassword = changePassOld.getText().toString();
String newPassword = changePassNew.getText().toString();
AsyncTask task = new UpdatePassword(oldPassword, newPassword, ProfileFragment.this.getContext());
task.execute();

Edit: I have noticed that i only have doInBackground but even when i had preExecute, it still wouldn't work

Jim Bergman
  • 5,207
  • 2
  • 17
  • 19
SkintMedia
  • 33
  • 1
  • 5
  • have you debugged your `postExecute()` method? are you sure that there is no return from `doInBackground` – Atef Hares Feb 22 '17 at 21:59
  • Yeah i have and returns nothing – SkintMedia Feb 22 '17 at 22:00
  • Then your problem is definitely in your `doInBackground` body, it could return null, debug it and see where the problem is! – Atef Hares Feb 22 '17 at 22:04
  • I've tried but that's what i don't understand, nothing comes up, so it doesn't even seem to be reaching doInBackground, which makes me wonder if I've missed something – SkintMedia Feb 22 '17 at 22:12
  • I don't see anything strange or I myself don't do with `AsyncTask` except the `static` way of implementation! I hope someone figures out what the problem is – Atef Hares Feb 22 '17 at 22:18
  • Okay, thanks for your help anyway. It'll no doubt be a really simple fixed which will all become clear once i'ts done lol – SkintMedia Feb 22 '17 at 22:20
  • `apiResponse.getStatus()`... Why are you returning this? That is not your new password. Anyways, your AsyncTask looks fine. Please show the code where you actually expect the value of `doInBackground` or `onPostExecute` to return to. – OneCricketeer Feb 22 '17 at 22:22

1 Answers1

0

AsyncTask doesn't always call but when it does, it comes up null?

It is the most interesting AsyncTask ;)


Generally speaking, you seem to not be returning the data to the point where you expect it (or a return null condition is being hit).

You can instead define a callback.

public interface PasswordChangeListener {
    void onPasswordChanged(String oldPass, String newPass);
}

Implement that on your Fragment class

public class ProfileFragment extends Fragment 
    implements PasswordChangeListener { // See here

    ...

    @Override
    public void onPasswordChanged(String oldPass, String newPass) {
        Toast.makeText(getActivity(), 
            String.format("Changed %s to %s", oldPass, newPass), 
            Toast.LENGTH_SHORT).show();
    }

Call your AsyncTask after you add that callback as a parameter

(Context usually goes first, by the way)

new UpdatePassword(
    ProfileFragment.this.getActivity(), 
    oldPassword, newPassword, 
    ProfileFragment.this).execute();

Suggestion: You should return apiResponse; from doInBackground

And implement onPostExecute on the AsyncTask with that suggestion in mind

@Override 
public void onPostExecute(ApiResponse result) {
    if (apiResponse != null && apiResponse.isSuccess()) {
        if (this.callback != null) {
            callback.onPasswordChanged(this.oldPassword, this.newPassword);
        } else {
            Log.w("Password change", "Password callback not set!");
        }
    } else {
        // TODO: Error handling
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245