0

I request xml from url and parse it in Async task, but when i start async task, progress dialog shows, loading rotating for 1 second, then stop animating, and i can't cancel the progress dialog, it continues till async task finishes. Here is my code:

private class UpdateTask extends AsyncTask<Void, Integer, InputStream> {
        private Context mContext;
        ProgressDialog pd;

        public UpdateTask(Context context) {
            mContext = context;
        }

        protected void onPreExecute() {
            pd = ProgressDialog.show(MainPanelActivity.this, "", getString(R.string.updatingProductsMsg, true, true,
                    new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            updateTask.cancel(true);
                            dialog.dismiss();
                        }
                    }));
        }

        protected InputStream doInBackground(Void... params) {
            InputStream is = null;
            int i = 1;
            try {
                while (is == null) {
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://www.myUrl.com/service.php");

                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                    nameValuePairs.add(new BasicNameValuePair("type", "product"));
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();

                    is = httpEntity.getContent();
                    System.out.println("====== Retry " + i + " =====");
                    i++;
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return is;
        }

        protected void onPostExecute(InputStream is) {
            MedicineParser medicineParser = new MedicineParser();
            List<Product> medicineList = null;
            try {
                medicineList = medicineParser.parse(is);
            } catch (XmlPullParserException | IOException e) {
                e.printStackTrace();
            }

            productListPref.saveMedicineList(mContext, medicineList);

            long unixTime = System.currentTimeMillis() / 1000L;
            updateDatabasePref.saveDatabaseVersion(mContext, unixTime);
            shoppingCartPref.emptyShoppingCart(mContext);

            Date date = new Date(unixTime * 1000L);
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", java.util.Locale.getDefault());
            String format = df.format(date);

            databaseUpdateDate.setText(format);

            if (pd.isShowing()) {
                pd.dismiss();
            }
        }
    }
kakajan
  • 2,614
  • 2
  • 22
  • 39

0 Answers0