簡體   English   中英

即使插入數據后,進度對話框仍會繼續旋轉

[英]progress dialog keeps on spinning even after inserting data

我只是在創建一個簡單的注冊模塊。 我已經編寫了以下代碼,它可以插入數據,但是進度對話框會繼續旋轉,並且永遠不會停止,但是如果我檢查數據庫,則數據已正確插入。 我正在使用凌空框架。 我是Android Volley的初學者,請告訴我我要去哪里了。

注冊.java

public class registration extends AppCompatActivity {
EditText Name,Email,Username,Password;
ProgressDialog dialog;
ProgressBar progressBar;
String Reg_url="------";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);
    Name=(EditText)findViewById(R.id.nametext);
    Email=(EditText)findViewById(R.id.email);
    Username=(EditText)findViewById(R.id.Username);
    Password=(EditText)findViewById(R.id.pass);
    dialog=new ProgressDialog(this);
    dialog.setTitle("Loading");
    dialog.setMessage("Please Wait a Momment");

    dialog.setCancelable(false);
}
public void SignUp(View view)
{
    if(TextUtils.isEmpty(Name.getText().toString()))
    {
        Name.setError("At least 5 charachters");
    }
    else if(TextUtils.isEmpty(Email.getText().toString()))
    {
        Email.setError("Enter a Valid Email");
    }
   else if(TextUtils.isEmpty(Username.getText().toString()))
    {
        Username.setError("At least 5 charachters");
    }
    else if(TextUtils.isEmpty(Password.getText().toString()))
    {
        Password.setError("At least 5 charachters");
    }
    else
    {
        dialog.show();
        StringRequest stringRequest=new StringRequest(Request.Method.POST, Reg_url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONArray jsonArray=new JSONArray(response);
                            JSONObject jsonObject=jsonArray.getJSONObject(0);
                            dialog.dismiss();
                            finish();
                            startActivity(new Intent(registration.this,MainActivity.class));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dialog.dismiss();
                Toast.makeText(registration.this,"Connection Failed",Toast.LENGTH_LONG).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<String, String>();
                map.put("name",Name.getText().toString());
                map.put("email",Email.getText().toString());
                map.put("user_name",Username.getText().toString());
                map.put("password",Password.getText().toString());

                return map;
            }
        };
        int socketTimeout= 30000;
        RetryPolicy policy=new DefaultRetryPolicy(socketTimeout,DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

}
}

我在清單文件中添加了Internet用戶權限。 我有凌空框架依賴凌空1.1.0。 它正確顯示了onerrorresponse,但如果我應用了一個不會顯示任何內容但已插入數據的吐司,則會作為響應。

我的PHP代碼是:

<?php
require "connect.php";
$name = $_POST["name"];
$email =$_POST["email"];
$user_name =$_POST["user_name"];
$password =$_POST["password"];
$sql = "INSERT INTO `user_info`(`name`, `email`, `user_name`, `password`) VALUES ('$name','$email','$user_name','$password')";
$result = mysqli_query($con,$sql);
mysqli_close($con);
?>

像這樣更改您的響應塊

                @Override
                public void onResponse(String response) {
                    Toast.makeText(registration.this,"Response :"+response,Toast.LENGTH_SHORT).show();
                    try {
                        JSONArray jsonArray=new JSONArray(response);
                        dialog.dismiss();
                        finish();
                    startActivity(new Intent(registration.this,MainActivity.class));                                                                                
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

編輯: Configure your php code in such a way that it returns a response during a network call. And then parse the response in the app side. Currently your try block is not getting executed due to no response from backend(even if the backend query is executed) Configure your php code in such a way that it returns a response during a network call. And then parse the response in the app side. Currently your try block is not getting executed due to no response from backend(even if the backend query is executed)

PHP代碼

    <?php
require "connect.php";
$name = $_POST["name"];
$email =$_POST["email"];
$user_name =$_POST["user_name"];
$password =$_POST["password"];
$sql = "INSERT INTO `user_info`(`name`, `email`, `user_name`, `password`) VALUES ('$name','$email','$user_name','$password')";
$result = mysqli_query($con,$sql);
$myArray = array();
if($result){
$myArray = array("status" => "success", 'code' => "200")
} else{
$myArray = array("status" => "failure", 'code' => "400")
}
echo json_encode($myArray);
mysqli_close($con);
?>

您的try / catch塊可能失敗。 通過以下方法進行測試,以確保是這種情況:a)在try / catch塊上設置一個斷點,並在調試器中逐行運行它,直到找到引起錯誤的那一行為止;或者b)向其中添加一條log語句包含e.getLocalizedMessage()的catch塊,這樣您就可以看到錯誤輸出,或者c)只是將dialog.dismiss()放在catch黑色中,看看它是否消失了。 如果您收到錯誤消息,但不知道它的含義,則將其張貼到底部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM