How to check if the last Asynktask is over

Asked

Viewed 32 times

0

I have an android application that communicates with the site through json that is obtained in the following way:

@Override
protected String doInBackground(String... params) {

    String url = params[0];
    String jsonString = params[1];

    Log.d("Parameters", " Url : " + url + " JSON :" + jsonString);

    try {

        if (client == null) {
            client = ((ProjectApplication) this.activity.get().getApplication()).getClient();
        }


        HttpContext context = new BasicHttpContext();


        CookieStore store = ((DefaultHttpClient) client).getCookieStore();
        context.setAttribute(ClientContext.COOKIE_STORE, store);

        HttpPost post = new HttpPost(url);
        post.setHeader("Content-type", "application/json");
        post.setHeader("Accept", "application/json");

        StringEntity stringParams = new StringEntity(jsonString);

        post.setEntity(stringParams);
        HttpResponse httpResponse;

        httpResponse = client.execute(post);


        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();


    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        is.close();
        json = sb.toString();
        reader.close();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }


    return json;

}

Then I take the variable json, and depending on the keyword in json, I launch an Activity on onPostExecute. The problem starts when I make two of these calls, the program does not wait for the end of the previous call before executing the current one. How can I fix it? I wanted to store somewhere Status last call to see if this is over, if it is not over then the current call is dropped.

Note: I don’t want to rule out the first Asynktask, I want to rule out the second.

  • I always use the asynctask.onPostExecute(), That didn’t work for you?

  • How does the second call start? It’s by a button?

  • I had already asked this question, and I forgot, how can I delete the previous one, @ramaral? (the two questions are mine...) the second call (onPostExecute) is automatically called when doInBackground (which indirectly is always called by a button) ends.

  • She already has an answer that answers that question. This is duplicate because the way it is, if I were to answer, the answer would be that.

  • I couldn’t figure out where the second call was made.

  • the second call is made indelibly , as the first, through a button. Imagine that the user has clicked on a tab1 (starts an Asynktask), this one has not loaded the content and it is already navigating to tab 2( what starts a new Asynktask, while we still have the previous Asynktask running to get the data from tab1). I hope now you understand what I’m trying to do.

  • As it is said in the given answer use the method getStatus() to know if Asynctask is finished. Depending on what you want, for example, you can do the disable of tab 2 no onPreExecute() and its enable in the onPostExecute() or cancel the task when tab2 is chosen or something else, only you know what you want.

  • I hadn’t picked it up this afternoon, but tomorrow I’ll try to fix it, and if I can, I’ll put the answer here

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.