Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中使用截取将json数组发布到使用JsonArrayRequest的Mysql数据库?_Java_Php_Android - Fatal编程技术网

Java 在android中使用截取将json数组发布到使用JsonArrayRequest的Mysql数据库?

Java 在android中使用截取将json数组发布到使用JsonArrayRequest的Mysql数据库?,java,php,android,Java,Php,Android,这是我为JsonArrayRequest编写的代码,我认为它不完整或不正确。问题是数据库中没有添加任何内容,我也没有收到任何错误。我是编程新手,所以我不知道我可能会出什么问题 private void insertToDb() { JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, INVEST_URL, itemSelectedJson, new Respon

这是我为
JsonArrayRequest
编写的代码,我认为它不完整或不正确。问题是数据库中没有添加任何内容,我也没有收到任何错误。我是编程新手,所以我不知道我可能会出什么问题

private void insertToDb() {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, INVEST_URL,
            itemSelectedJson, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Toast.makeText(AddInvEst.this, "The echoed response is "+response, Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers = new HashMap<String, String>();
            headers.put("Content-Type","application/json");
            headers.put("Accept", "application/json");
            return headers;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}
这是我的php代码

<?php
require "init.php";
$json = file_get_contents('php://input'); 
//$data = json_decode($json,true);
// remove the ,true so the data is not all converted to arrays
$data = json_decode($json);
// Now process the array of objects
foreach ( $data as $inv ) {
    $custInfo = $inv->custInfo;
    $rate =     $inv->rate;
    $weight=    $inv->weight;
    $desc=      $inv->desc;
    $makingAmt= $inv->makingAmt;
    $vat=       $inv->vat;
    $itemTotal= $inv->itemTotal;
    $sum_total= $inv->sum_total;
    $barcode=   $inv->barcode;
    $net_rate=  $inv->net_rate;
    $date=      $inv->date;
    $invoiceNo= $inv->invoiceNo;
    $bill_type= $inv->bill_type;
    $sql = "INSERT INTO selected_items 
             (custInfo, invoiceNo, barcode, desc, 
              weight, rate, makingAmt,net_rate,
              itemTotal,vat,sum_total,bill_type,date) 
            VALUES
             ('$custInfo','$invoiceNo','$barcode','$desc',
              '$weight','$rate','$makingAmt','$net_rate',
              '$itemTotal','$vat','$sum_total','$bill_type','$date')";
    $res = mysql_query($sql,$con);
    if(!$res){
        $result = new stdClass();
        $result->status = false;
        $result->msg = mysql_error();
        echo json_encode($result);
        exit;
    }
}
?>

您没有在post请求中添加方法getParams(),它应该具有以下结构

StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    errorHandler(error);
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            //Adding parameters to request
            params.put("KEY1", key1);
            params.put("KEY2", key2);

            //returning parameter
            return params;
        }
    };

    //Adding the string request to the queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
StringRequest StringRequest=新的StringRequest(Request.Method.POST,“url”,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
errorHandler(错误);
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
//向请求添加参数
参数put(“键1”,键1);
参数put(“键2”,键2);
//返回参数
返回参数;
}
};
//将字符串请求添加到队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}

desc
是mysql中的保留关键字,必须在backtick中!!日期。。相同(最好都倒勾)@Saty谢谢我会改变的,但是你说的倒勾是什么意思though@Svetlio
date
是关键字,但不是保留的检查没有(R)@Saty您是对的,但这不会改变我的建议。。顺便说一句,PHP已经删除了Mysql库,所以最好改用mysqli或pdo。我试图发送一个jsonArray而不是字符串。这是一样的,您只需更改jsonArray请求中显示StringReques的位置,并将其放入侦听器中。有关更多信息,请查看:
StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    errorHandler(error);
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            //Adding parameters to request
            params.put("KEY1", key1);
            params.put("KEY2", key2);

            //returning parameter
            return params;
        }
    };

    //Adding the string request to the queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}