Progressdialog 不会在齐射响应块内被解雇

Progressdialog is not getting dismissed inside the volley response block

我的应用程序向后端执行上传操作 php server.My 问题是我无法关闭 volley Onresponse() method.Onresponse() 方法中的进度对话框并在后台显示 toast Progressdialog,这意味着 Onresponse() 被当前调用。

我在 public

中声明了 ProgressDialog 变量

onCreate() 方法如下所示

    ProgressDialog dialog;
    protected void onCreate(Bundle savedInstanceState) 
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.signup);

            btn.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                      dialog=new ProgressDialog(context,ProgressDialog.STYLE_SPINNER);
                      dialog.setIndeterminate(true);
                      dialog.show(context,"Signing up","Please wait...");
                      login_call();
             }

Login_call() 执行截击请求,在 onResponse() 中我需要关闭对话框,但它不起作用。

    public void login_call() 
    {
         RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
         StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.56.1/sign.php", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if (response.equals("success")) 
            {

                dialog.dismiss();

                getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            } 
            else 
            {
                dialog.dismiss()
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            dialog.dismiss();
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }


    ) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> param = new HashMap<>();
            param.put("user",user.getText().toString());
            param.put("pass",pass.getText().toString());
            param.put("name",name.getText().toString());
            param.put("mob",mob.getText().toString());
            param.put("email",email.getText().toString());
           param.put("photo",photo);
            return param;
        }
    };

    queue.add(request);
 }

Toast 消息显示正确,但进度对话框未关闭。

有什么解决问题的办法吗??

先到先你没有检查所有条件

new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        if (response.equals("success")) {
         dialog.dismiss();
            getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        **} else {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
        }**
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       dialog.dismiss();
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
    }

else 块不隐藏对话框,因此您应该以这种方式更改它

new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        dialog.dismiss();
        if (response.equals("success")) {
            getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        } else {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       dialog.dismiss();
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
    }

我认为错误是这个,当然假设你的对话框可以从回调中访问

只要改变这个:

RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.56.1/sign.php", new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

        dialog.dismiss(); // write it there

        if (response.equals("success")) {

       // dialog.dismiss(); remove this from here..!!


            getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

            );
        } else {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       dialog.dismiss();
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
    }
}

Edit : set this

dialog.setIndeterminate(false);

更改您的 Dialog.show 方法以使用此版本:show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable) 像这样:

final ProgressDialog dialog = ProgressDialog.show(context, "Signing up","Please wait...", false, true); 

因此您的 onClick 代码应更改为如下内容:

public void onClick(View view) {
    final ProgressDialog dialog = ProgressDialog.show(context, "Signing up","Please wait...", false, true); 
    login_call();
     }

请试一试,如果有帮助请告诉我。