0

How to catch moment when AsyncTask is finished? I have ProfileUpdate class which extends AsyncTask, and from another activity I calling this method and after I need update my data. How to know that asynctask finished? My asynctask method in another class and not in activity class!!!

this is my onRefresh method in the activity:

@Override
public void onRefresh() {
    if (!AlertView.isInternetAvailable(getContext())) {
        swipeLayout.setRefreshing(false);
        Toast.makeText(getContext(), Messages.CONNECTION_ERROR + ": " + Messages.NO_INTERNET, Toast.LENGTH_SHORT).show();
    } else {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new UpdateProfile(getActivity(), swipeLayout, sharedPreferences.getString(Constants.TOKEN, ""), user.getIin()).execute(Urls.GET_USER);
            }
        });
        profileDefaults();
    }
}

and here my AsyncTask method:

public class UpdateProfile extends AsyncTask <String, Void, String> {
private Activity activity;
private SwipeRefreshLayout swipeRefreshLayout;
private String token;
private String userIin;
private SharedPreferences sharedPreferences;

public UpdateProfile(Activity activity, SwipeRefreshLayout swipeRefreshLayout, String token, String userIin) {
    this.activity = activity;
    this.swipeRefreshLayout = swipeRefreshLayout;
    this.token = token;
    this.userIin = userIin;
    sharedPreferences = this.activity.getSharedPreferences(Constants.PROJECT, Context.MODE_PRIVATE);
}

@Override
protected String doInBackground(String... params) {
    URL url = null;
    try {
        url = new URL(params[0]);
        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            RequestBody body = new FormBody.Builder()
                    .add("iin", userIin)
                    .build();

            Request request = new Request.Builder()
                    .url(url)
                    .addHeader(Constants.AUTH_TOKEN, token)
                    .post(body)
                    .build();
            Response responses = null;
            try {
                responses = okHttpClient.newCall(request).execute();
            } catch (Exception e) {
                AlertView.showAlertView(activity, Messages.CONNECTION_ERROR, Messages.NO_INTERNET, Messages.OK);
            }

            assert responses != null;
            return responses.body().string();

        } catch (Exception e) {
            AlertView.showAlertView(activity, Messages.CONNECTION_ERROR, Messages.NO_INTERNET, Messages.OK);
        }
    } catch (Exception e) {
        AlertView.showAlertView(activity, Messages.CONNECTION_ERROR, Messages.NO_INTERNET, Messages.OK);
    }
    return null;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    try {
        JSONObject jsonObject = new JSONObject(s);
        int code = Integer.valueOf(jsonObject.getString(Constants.CODE));
        if (code == Codes.OK) {
            Toast.makeText(activity, "Данные обновлены", Toast.LENGTH_SHORT).show();

            String userHeader = jsonObject.getString("user");
            JSONObject userInfo = new JSONObject(userHeader);

            String mobilePhone = userInfo.getString(SingletonConstants.MOBILE_PHONE);
            Boolean isActive = userInfo.getBoolean(SingletonConstants.IS_ACTIVE);
            Boolean signedAgreement = userInfo.getBoolean(SingletonConstants.SIGNED_AGREEMENT);
            Boolean esfEntered = userInfo.getBoolean(SingletonConstants.ESF_ENTERED);
            String address = userInfo.getString(SingletonConstants.ADDRESS);
            String iin = userInfo.getString(SingletonConstants.IIN);
            String certExpDate = userInfo.getString(SingletonConstants.CERT_EXP_DATE);
            String firstName = userInfo.getString(SingletonConstants.FIRST_NAME);
            String middleName = userInfo.getString(SingletonConstants.MIDDLE_NAME);
            String workPhone = userInfo.getString(SingletonConstants.WORK_PHONE);
            String secondName = userInfo.getString(SingletonConstants.SECOND_NAME);
            String avatarUrl = userInfo.getString(SingletonConstants.AVATAR_URL);;
            String secondEmail = userInfo.getString(SingletonConstants.SECOND_EMAIL);
            String email = userInfo.getString(SingletonConstants.EMAIL);

            User newUser = new User(mobilePhone, isActive, signedAgreement, esfEntered, address, iin, certExpDate, firstName, middleName, workPhone, secondName, avatarUrl, secondEmail, email);

            Gson gson = new Gson ();
            String userGson = gson.toJson (newUser);
            sharedPreferences.edit().putString(SingletonConstants.USER, userGson).apply();
            swipeRefreshLayout.setRefreshing(false);

        } else {
            AlertView.showAlertView(activity, Messages.ERROR, jsonObject.getString(Constants.MESSAGE), Messages.OK);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

How I can call profileDefaults()? It's into my activity. And I need to call it after onPostExecute!

Baktiyar Bekbergen
  • 374
  • 2
  • 4
  • 24
  • Why not make the class which handles your async task an inner class of the activity which consumes it? This is the pattern I have used before, and I have never had your problem. – Tim Biegeleisen Oct 22 '17 at 14:27
  • 1
    @TimBiegeleisen Yes, of course I can write this asyncTask inside my activity, but I have several asynctask method inside this activity, and It' really many code, and I want just divide each asynctask method in different class – Baktiyar Bekbergen Oct 22 '17 at 14:30
  • Can you pass a reference to the calling activity and then assign some class level variables using it? – Tim Biegeleisen Oct 22 '17 at 14:42
  • @TimBiegeleisen this method I to update profile, and here I have user class which have many data, and I think it's not so comfortable to send in constructor everything every time – Baktiyar Bekbergen Oct 22 '17 at 14:51

1 Answers1

1

A cleaner way to do it is to use interfaces as listeners. Create this interface:

public interface OnAsyncFinished{
    void onAsyncFinished(Object o);
}

Add the interface as a parameter in your AsyncTaskClass constructor:

private OnAsyncFinished onAsyncFinished;
public UpdateProfile(..., OnAsyncFinished onAsyncFinished) {
   ...
   this.onAsyncFinished = onAsyncFinished;
}
...
@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    onAsyncFinished.onAsyncFinished(s); //This will notifiy the method on main activity, and you can now resume the work there
    ...
}

Implement the interface in your main activity:

public MainActivity extends Activity implements OnAsyncFinished {
     @Override
     public void onAsyncFinished(Object o) {
         //This will get called after onPostExecute, do what u want with the object you got from onPostExecute, json or string in ur example
         profileDefaults(); //call ur function
     }
}

EDIT: When creating the async task in your main activity pass this in the constructor likeso:

        @Override
        public void run() {
            new UpdateProfile(..., this).execute(Urls.GET_USER);
        }
riadrifai
  • 1,108
  • 2
  • 13
  • 26