Can not send data to server using volley android

advertisements

I want to make a user registration page using volley but I am unable to send data to the server. It's the first time I am using volley. I have included all the permissions and server is also running appropriately, I am just unable to send the data to the online database.

When I run it, it gives the following message: "warning mysqli prepare() expects parameter 1 to be mysqli, boolean given in home..."

Please help. Here's my android code: User class contains String of name, username, mobile, password.

private void registerUser(final User user) {
    StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVER_ADDRESS + "Register.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(Register.this,response,Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Register.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("name",user.name);
            params.put("username", user.username);
            params.put("mobile", user.mobile);
            params.put("password", user.password);
            Log.d(name+username+"her", "her");
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}
}

And here's the php file:

 <?php

if($_SERVER['REQUEST_METHOD']=='POST'){
$con=mysqli_connect("localhost","dbName","pass","userName");

$name = $_POST["name"];
$username = $_POST["username"];
$mobile = $_POST["mobile"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "INSERT INTO UserDetails (name, username, mobile, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "ssss", $name, $username, $mobile, $password);
mysqli_stmt_execute($statement);
echo $name, $username, $mobile, $password;
mysqli_stmt_close($statement);

mysqli_close($con);}
 ?>


You need to start the queue before adding any request to it.

 RequestQueue requestQueue = Volley.newRequestQueue(this);
 requestQueue.start()
 requestQueue.add(stringRequest);

Please make sure that you call start() only once. Hence create a singleton class VolleyManager, when you create a instance of this class it should create a request queue and start it. For each API call just call VolleyManager.getInstance().getQueue().add(stringRequest)