Crawler
Crawler

Reputation: 2018

Progress Dialog is not shown during execution of AsyncTask

I googled for hours and tried all answers of Stack Overflow but didn't solved my problem.

I made android program to download data in bytes which wroks perfectly. And for download working on background thread. I want to show a Progress Dialog(jus Spinning one). But I'm having really annoying problem. My download takes about 5 sec but my progress dialog is either not shown or just shown for last 1 sec.

Here's my code for AsyncTask.

public class ImageJSON extends AsyncTask<String, String, byte[]> {

private JSONObject jsonObject = new JSONObject();
private byte[] response;

 private ProgressDialog pg; 
 private Context context; 

 public ImageJSON(Activity activity) {
     pg = new ProgressDialog(activity);
 }
@Override 
protected void onPreExecute() { 
    super.onPreExecute();
    pg.setMessage("Downloading, please wait.");
    pg.show(); 
} 

@Override 
protected void onProgressUpdate(String... progress) {
    super.onProgressUpdate(progress);
    if (pg != null) { 
        pg.setProgress(Integer.parseInt(progress[0])); 
    } 
}

protected void onPostExecute(byte[] result) { 
    if (pg.isShowing()) { 
        pg.dismiss(); 
    } 
}
@Override
protected byte[] doInBackground(String... params) {
    HttpPost httpPost = new HttpPost("my file url....");
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext httpContext = new BasicHttpContext();
    try {
        jsonObject.put("imageId", params[0]);

        StringEntity stringEntity = new StringEntity(jsonObject.toString());
        httpPost.addHeader("Content_Type", "application/octet_stream");
        httpPost.setEntity(stringEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost, httpContext);
        HttpEntity entity = httpResponse.getEntity();

        if (entity!=null) {
            response = EntityUtils.toByteArray(entity);
            entity.consumeContent();
            httpClient.getConnectionManager().shutdown();
        }
    } catch (JSONException|IOException e) {
        e.printStackTrace();
    }
    return response;
}

And I called it from my activity using this code.

final ImageJSON imageTask = new ImageJSON(ReaderActivity.this);
byte[] response = imageTask.execute(imageId).get();

If anybody can help me, Thanks in advance.

Upvotes: 0

Views: 104

Answers (4)

Piyush
Piyush

Reputation: 18923

Change this

public ImageJSON(Context ctx) {
 this.context = ctx;

}

Now in your onPreExecute() method use this

@Override 
protected void onPreExecute() { 
 super.onPreExecute();
 pg = new ProgressDialog(context);
 pg.setMessage("Downloading, please wait.");
 pg.show(); 
 } 

You'r initializing your ProgressDialog in ImageJSON class which block your UI thread means freezes it. So need to initialize it in onPreExecute() method with reference of context.

Upvotes: 0

you forgot to initilise your progressDialogue in onPreExecute method

@Override 
protected void onPreExecute() { 
    super.onPreExecute();
pg = new ProgressDialog(pass_context_here);
    pg.setMessage("Downloading, please wait.");
    pg.show(); 
} 

Upvotes: 0

cliffroot
cliffroot

Reputation: 1691

You don't want to be using get() as it still freezes your UI thread thus denying the sense of using AsyncTask. You might want to implement onPostExecute in order to return your result properly.

Upvotes: 3

yuva ツ
yuva ツ

Reputation: 3703

try this -

public ImageJSON(Activity activity) {

 }
@Override 
protected void onPreExecute() { 
    super.onPreExecute();
pg = new ProgressDialog(ReaderActivity.this);
    pg.setMessage("Downloading, please wait.");
    pg.show(); 
} 

Upvotes: 0

Related Questions