KuKeC
KuKeC

Reputation: 4610

How to display a ProgressDialog in an AsyncTask?

My Android app got one AsyncTask which gets me data from my server. When I pull a few rows from the database then it's done very fast and I don't need a loading animation while it is getting fresh data.

When I pull 2,3k rows from the database, that might slow things down so I decided to use some indicator (loading animation), so the user knows that data is collecting in the background. I got one activity Fill_in_phone where I call the asyncTask named GetOcitanja.

My code for the AsynTask is:

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;

public class GetOcitanja extends AsyncTask<Object, Void, String> {

Activity  _context;
String _str_mesg;
String _str_naslov;
public ProgressDialog progress;
public GetOcitanja(Activity context, String str_naslov, String str_message){
    this._context = context;
    this._str_naslov = str_naslov;
    this._str_mesg = str_message;
}

@Override
protected void onPreExecute() {
    //super.onPreExecute();
    progress = ProgressDialog.show(_context, _str_naslov,
            _str_mesg, true);
    progress.show();

}

@Override
protected void onPostExecute(String s) {
    //super.onPostExecute(s);
    progress.dismiss();
}

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

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpRequest = new HttpGet(Config.url_get_ocitanja_async_taks);
    String odg="";


    try {
        HttpResponse response = httpClient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        odg = EntityUtils.toString(entity, HTTP.UTF_8);

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


    return odg;
}

As you can see, I put a 2 seconds sleep time to simulate a large dataset. I call this AsyncTask in my Fill_in_Data activity:

GetOcitanja asyncTask=new GetOcitanja(Fill_in_phone.this, "a","b");
asyncTask.execute();
String response="";
try {
        response= asyncTask.get();
    } catch (InterruptedException e1) {

        e1.printStackTrace();
    } catch (ExecutionException e1) {
        e1.printStackTrace();
    }
}

I followed a few solutions from SO and nothing helped. What did I do wrong?

Upvotes: 0

Views: 96

Answers (3)

greenapps
greenapps

Reputation: 11214

  response= asyncTask.get();

remove that.

You have already an asyncTask.execute().

Handle the response in onPostExecute().

I asked you before how you called your async task as i supposed you used .get(). You are calling it twice and are only showing that now.

Upvotes: 1

Tixeon
Tixeon

Reputation: 493

public GetOcitanja(Activity context, String str_naslov, String str_message){
    this._context = context;
    this._str_naslov = str_naslov;
    this._str_mesg = str_message;
    progressDialog = new ProgressDialog(_context);
    progressDialog.setTitle("Progress");

}

can you try create progress dialog inside constructor

Upvotes: 1

Mohammad Tauqir
Mohammad Tauqir

Reputation: 1815

Place your ProgressDialog in onPreExecute, sample code below:

private ProgressDialog pdia;

@Override
protected void onPreExecute(){ 
   super.onPreExecute();
        pdia = new ProgressDialog(yourContext);
        pdia.setMessage("Loading...");
        pdia.show();    
}

@Override
protected void onPostExecute(String result){
   super.onPostExecute(result);
        pdia.dismiss();
}

https://stackoverflow.com/a/25998219/5202007

Upvotes: 1

Related Questions