Search code examples
androidandroid-asynctaskprogressdialog

Progressbar not working inside AsyncTask


I am using an AsyncTask to download some big amount of data from a server my AsyncTask work fine so i added a progress bar to make everything beautiful but problem is when its running progress bar get freeze half way down, and i use progress dialog that also the same its freeze half way down,

private class downloadChannelsfromserver extends
        AsyncTask<String, Void, String> {

     ProgressDialog progressDialog;

    @Override
    protected String doInBackground(String... url) {
        String data = "";

        try {
            // Fetching the data from web service
            data = getLinksfromServer(url[0]);
        } catch (Exception e) {
            Log.d("Background Task", e.toString());
        }
        return data;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
         progressDialog= ProgressDialog.show(Settings.this, "Synchronicing","Synchronicing", true);


    }



    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        JSONObject json;
        try {
            json = new JSONObject(result);
            db.deleteAll();
            final JSONArray jsonArray = json.getJSONArray("XXXX");

            for (int i = 0; i < jsonArray.length(); i++) {
                                ///use for insert datainto database
            }
            finish();

            progressDialog.dismiss();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.e("settings", "", e);
            progressDialog.dismiss();

        }
    }

can and someone tell me why this happen, Pls


Solution

  • Reason for progress bar get stuck was my JSON decoding since onPostExecute belongs to UI thread it take several time to decode the json results. so it will freeze the UI until JSON decode so move the decoding part to doInBackground will solve the UI freeze issue since doInBackground belongs to background thread