ProgressDialog getting Dismissed before Toast appears from doInBackground method of AsyncTask

662 views Asked by At

I'm making a JSON HTTP request using AsyncTask. I have created a ProgressDialog in onPreExecute() and i'm dismissing it in onPostExecute(). The problem is, there is a Toast message in diInBackground()which shows the JSON response and ProgressDialog is being dismissed before the Toast appears, meaning onPostExecute() is being called before the Toast is generated. How can I dismiss it just before the Toast appears?

Here is my code.

private  class login extends AsyncTask<Void,Void,Void>{
      private ProgressDialog progressDialog;

      @Override
      protected void onPreExecute(){
          super.onPreExecute();
          // Create & Start Progress Dialog
          progressDialog = new ProgressDialog(LoginActivity.this);
          progressDialog.setMessage("Loading...");
          progressDialog.show();
      }
      @Override
      protected Void doInBackground(Void... params) {
          String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
          JsonObjectRequest req = new JsonObjectRequest(url, null,
                  new Response.Listener<JSONObject>() {

                      @Override
                      public void onResponse(JSONObject response) {

                          try {

                                  Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();

                              }

                          }
                          catch (Exception e){
                              Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();

                          }


                      }
                  }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  VolleyLog.e("from on error response Server Error..: ", error.getMessage());

              }
          });

          AppController.getInstance().addToRequestQueue(req);

            return null;
      }
      @Override
      protected void onPostExecute(Void v){
          // Terminate Progress Dialog
          progressDialog.dismiss();
      }
      }
5

There are 5 answers

0
android_dev On BEST ANSWER

No Need to write the Volley Request in Async Task. You can Directly Excute that task because internaly it's using thread. So Once you got response or Failure so u can close the dialog there it self. I guess it will helps you

 String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
      JsonObjectRequest req = new JsonObjectRequest(url, null,
              new Response.Listener<JSONObject>() {

                  @Override
                  public void onResponse(JSONObject response) {

      if(progressDialog.isShowing){
     progressDialog.cancel();
       }
                      try {

                              Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();

                          }

                      }
                      catch (Exception e){
                          Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();

                      }


                  }
              }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              VolleyLog.e("from on error response Server Error..: ", error.getMessage());
  if(progressDialog.isShowing){
 progressDialog.cancel();
   }
          }
      });

      AppController.getInstance().addToRequestQueue(req);

I hope this will helps you And no need of AsyncTask.

0
jeet parmar On

you want print toast message then you can use Handler.

   Handler h = new Handler(this.getMainLooper());
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();
                        //stopProgress 
                    }
                });
0
Amsheer On
try {

                              Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();

                          }

                      }
                      catch (Exception e){
                          Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();

                      }

This code completely wrong. You can not add Toast from doInBackground. . You need to move this after dismiss dioalog

// Terminate Progress Dialog
      progressDialog.dismiss();
//Add your Toast here
0
Entera de Aceite y Tomate On

You can dismiss the dialog manually after toast shows, with a TimerTask. You can see how to use TimerTask HERE. You are using Toast.LENGTH_SHORT, so the toast will show for 2 seconds.

0
Angad Tiwari On

dismiss the progressdialog inside response and error listener...no need to dismiss on post method

private  class login extends AsyncTask<Void,Void,Void>{
  private ProgressDialog progressDialog;

  @Override
  protected void onPreExecute(){
      super.onPreExecute();
      // Create & Start Progress Dialog
      progressDialog = new ProgressDialog(LoginActivity.this);
      progressDialog.setMessage("Loading...");
      progressDialog.show();
  }
  @Override
  protected Void doInBackground(Void... params) {
      String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
      JsonObjectRequest req = new JsonObjectRequest(url, null,
              new Response.Listener<JSONObject>() {

                  @Override
                  public void onResponse(JSONObject response) {

                      try {

                              Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();

                          }

                      }
                      catch (Exception e){
                          Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();
      progressDialog.dismiss(); add this line

                      }


                  }
              }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              VolleyLog.e("from on error response Server Error..: ", error.getMessage());
      progressDialog.dismiss(); add this line

          }
      });

      AppController.getInstance().addToRequestQueue(req);

        return null;
  }
  @Override
  protected void onPostExecute(Void v){
      // Terminate Progress Dialog
      //progressDialog.dismiss(); comment this line
  }
  }