如何使用Volley将数据插入Mysql数据库

时间:2016-09-03 12:36:55

标签: php mysql android-studio android-volley

我正在尝试使用volley将数据插入到mysql数据库中,但它总是响应为错误,我被卡住了,无法继续我的项目。我无法找到导致它不执行我的PHP代码的原因。我还使用PostMan检查了我的PHP代码,它运行良好。我现在正试图插入一些测试数据,但仍然没有运气。我希望你能为我发光并为我提供一些帮助。谢谢。

Insert.Java

Button confirmOrder = (Button) findViewById(R.id.confirmOrder);
        confirmOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final int a = 1;
                final String b = "Beth";
                final int c = 1;
                final int d = 3;
                final int e = 1;
                final String f = "Bert";



                final String URL = "https://10.0.2.2/myDB/order.php";

                StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if(response.contains("success")) {
                            Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(),"failed",Toast.LENGTH_SHORT).show();
                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        params.put("table_id", String.valueOf(a));
                        params.put("cust_name", b);
                        params.put("item_id", String.valueOf(c));
                        params.put("quantity", String.valueOf(d));
                        params.put("status_id", String.valueOf(e));
                        params.put("username", f);
                        return params;
                    }
                };

                MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

            }
        });

order.php

<?php
include_once("init.php");

if(isset($_POST['table_id']) && isset($_POST['cust_name']) && 
   isset($_POST['item_id']) && isset($_POST['quantity']) && isset($_POST['status_id'])
   && isset($_POST['username'])){
       $tableid = $_POST['table_id'];
       $custname = $_POST['cust_name'];
       $itemid = $_POST['item_id'];
       $quantity = $_POST['quantity'];
       $statusid = $_POST['status_id'];
       $username = $_POST['username'];

       $query = "INSERT INTO tbl_order(table_id,cust_name,item_id,quantity,status_id,username)
       VALUES ('$tableid', '$custname', '$itemid', '$quantity', '$statusid', '$username')";

     $result = mysqli_query($con, $query);

    if($result > 0){
        echo "success";   
    }
    else{
        echo "failed";   
    }
}


?>

2 个答案:

答案 0 :(得分:2)

该行 - final String URL = "https://10.0.2.2/myDB/order.php";

http://10.0.2.2/myDB/order.php";你拼错了http

答案 1 :(得分:1)

您未发送任何内容类型标头,如this tutorial

所示
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String,String> params = new HashMap<String, String>();
    params.put("Content-Type","application/x-www-form-urlencoded");
    return params;
}