ProgressDialog 在 Toast 出现之前从 AsyncTask 的 doInBackground 方法中消失

ProgressDialog getting Dismissed before Toast appears from doInBackground method of AsyncTask

我正在使用 AsyncTask 发出 JSON HTTP 请求。我在 onPreExecute() 中创建了一个 ProgressDialog,我在 onPostExecute() 中将其关闭。问题是,diInBackground() 中有一条 Toast 消息显示 JSON responseProgressDialogToast 出现之前被关闭,意思是 onPostExecute() 在生成 Toast 之前被调用。我怎样才能在 Toast 出现之前关闭它?

这是我的代码。

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();
      }
      }
try {

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

                          }

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

                      }

这段代码完全错误。 您不能从 doInBackground. 添加 Toast。你需要在关闭 dioalog 后移动它

// Terminate Progress Dialog
      progressDialog.dismiss();
//Add your Toast here

您可以在 toast 显示后使用 TimerTask 手动关闭对话框。您可以查看如何使用 TimerTask HERE。您正在使用 Toast.LENGTH_SHORT,因此吐司将显示 2 秒。

无需在Async Task中编写Volley Request。您可以直接执行该任务,因为它在内部使用线程。因此,一旦您收到响应或失败,您就可以自行关闭对话框。我想它会对你有所帮助

 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);

希望这对您有所帮助而且不需要 AsyncTask。

关闭响应和错误侦听器中的进度对话框...无需关闭 post 方法

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
  }
  }

你想要打印吐司消息然后你可以使用 处理程序.

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