1

I want to update download progress of progress dialog but AsyncTask getting hangs when using BufferedReader. Not calling publishProgress and not updating progress dialog in onProgressUpdate. Also, it's not printing Log into while loop.

I have tested using printing Log in my code.

  • First Log is printing in LogCat
  • Second Log is not printing
  • The Third Log also not printing and not executing method publishProgress.

    private class GetData extends AsyncTask<String, Integer, String>{
        private ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
            dialog = new ProgressDialog(HomeScreen.this);
            dialog.setTitle("Be Patient");
            dialog.setMessage("Loading data for first time use only. Please don't close the application.");
            dialog.setCancelable(false);
            dialog.setIndeterminate(true);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.show();
        }
    
        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
    
            dialog.setIndeterminate(false);
            dialog.setMax(100);
            dialog.setProgress(progress[0]);
        }
    
        @Override
        protected String doInBackground(String... params) {
    
            String serverResponse = null;
            BufferedReader in = null;
            java.net.URL myLink;
    
            try {
                Log.e("AsyncTask", "This log is printing");
                myLink = new java.net.URL(params[0]);
                in = new BufferedReader(new InputStreamReader(myLink.openStream()));
                Log.e("AsyncTask", "This log not printing");
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                    publishProgress(50);  // Not executing : Default value set is 50 just for cheecking.
                    Log.d("AsyncTask", "This log also not printing & publishProgress not executing");
                }
                serverResponse = response.toString();
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return serverResponse;
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
    
            if (!HomeScreen.this.isFinishing() && dialog != null) {
                dialog.dismiss();
            }
        }
    }
    
Sudarshan
  • 938
  • 14
  • 27
Chayan Mistry
  • 396
  • 3
  • 14

0 Answers0