简体   繁体   English

无法使用Volley Android将数据发送到服务器

[英]Unable to send data to server using volley android

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..." 当我运行它时,它会显示以下消息:“警告mysqli prepare()期望参数1为mysqli,在家中给定的布尔值...”

Please help. 请帮忙。 Here's my android code: User class contains String of name, username, mobile, password. 这是我的android代码:用户类包含名称,用户名,手机,密码的字符串。

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文件:

 <?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. 请确保只调用一次start()。 Hence create a singleton class VolleyManager, when you create a instance of this class it should create a request queue and start it. 因此,创建一个单例类VolleyManager,当您创建此类的实例时,应创建一个请求队列并启动它。 For each API call just call VolleyManager.getInstance().getQueue().add(stringRequest) 对于每个API调用,只需调用VolleyManager.getInstance()。getQueue()。add(stringRequest)

Add

global $con 全球$ con

in your php file. 在您的php文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM