我现在有点困惑,因为我在PHP方面提出了与JSON解码相关的问题。我专注于使用stringrequest解码来自Android Volley的JSONobject POSTED。我能够创建一个关于向MySql插入数据的好代码,我忘了我在客户端仍然有一个错误的配置。现在我得到“E / Volley:[215] BasicNetwork.performRequest:意外的响应代码500”我将在Android和PHP方面发布我的代码。请相应指导我。这里真的很累。希望有人帮助我。
客户端:
public void testOrder (ArrayList<String> order_id,
ArrayList<String> uname,
ArrayList<String> prod_name,
ArrayList<String> prod_id,
ArrayList<String> quantity,
ArrayList<String> branches,
ArrayList<String> totalPrice,
int itemIteration){
JSONObject obj = null;
JSONArray jsonArray = new JSONArray();
final JSONObject finalobject = new JSONObject();
for (int i = 0; i < itemIteration; i++) {
obj = new JSONObject();
Log.d("OBJECT_COUNTER", String.valueOf(i));
try {
obj.put("itemCount", itemIteration-1);
obj.put("order_id", order_id.get(i));
Log.d("ORDER_ID", order_id.get(i));
obj.put("uname", uname.get(i));
Log.d("USERNAME", uname.get(i));
obj.put("prod_name", prod_name.get(i));
Log.d("PROD_NAME", prod_name.get(i));
obj.put("quantity", quantity.get(i));
Log.d("PROD_QUANT", quantity.get(i));
obj.put("branch", branches.get(i));
Log.d("PROD_BRANCHES", branches.get(i));
obj.put("totalPrice", totalPrice.get(i));
Log.d("TOTAL_PRICE", totalPrice.get(i));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonArray.put(obj);
}
try {
finalobject.put("ORDERLIST", jsonArray);
test = jsonArray.toString();
// Toast.makeText(this,test,Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
StringRequest strRequest = new StringRequest(Request.Method.POST, Config.TRY_JSON_POST,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(PlaceOrder.this, response, Toast.LENGTH_SHORT).show();
//Log.d("Response",response);
//
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(PlaceOrder.this, error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("ORDERLIST", finalobject.toString());
return params;
}
};
RetryPolicy policy = new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); strRequest.setRetryPolicy(policy);
AppController.getmInstance().addToRequesQueue(strRequest);
}
}
服务器端:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$content = file_get_contents("php://input");
$response = json_decode($content, true);
$orderlist = json_decode($response['ORDERLIST'], true);
//$count = count($response);
for($x=0;$x<=count($response);$x++){
foreach($orderlist AS $row){
$id=$row[$x]['order_id'];
$uname=$row[$x]['uname'];
$query_parts[$x] = "("."'".$row[$x]['order_id']."'".","."'".$row[$x]['uname']."'".","."'".$row[$x]['prod_name']."'".","."'".$row[$x]['quantity']."'".","."'".$row[$x]['totalPrice']."'".")";
}
}
$sql_1 = "INSERT INTO `order` (`order_id`, `uname`) VALUES ('$id', '$uname')";
$sql_2 = "INSERT INTO orders (order_id, uname, prod_name, quantity, total_price) VALUES ".implode(',', $query_parts);
if ($con->query($sql_1) === TRUE) {
if ($con->query($sql_2) === TRUE) {
echo "New record created successfully";
} else {
echo "Error2: " . $sql_2 . "<br>" . $con->error;
}
} else {
echo "Error1: " . $sql_1 . "<br>" . $con->error;
}
} else {
echo "FAILED JSON POST";
}
首先删除了一些预防VOLLEY.ERROR的声明
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$content = file_get_contents("php://input");
//$response = json_decode($content, true);
//$orderlist = json_decode($response['tag'], true);
//$count = count($response);
$response = json_decode($content, true);
$orderlist= $response['ORDERLIST'];
echo $content;
} else {
echo "NO POST";
}
我得到了这些代码,所以我的客户服务器端收到了我的JSONObject
ORDERLIST=%7B%22ORDERLIST%22%3A%5B%7B%22itemCount%22%3A1%2C%22order_id%22%3A%226794%22%2C%22uname%22%3A%22jordan%22%2C%22prod_name%22%3A%22ABC%22%2C%22quantity%22%3A%221%22%2C%22branch%22%3A%22Dapitan%22%2C%22totalPrice%22%3A%2223500%22%7D%2C%7B%22itemCount%22%3A1%2C%22order_id%22%3A%226794%22%2C%22uname%22%3A%22jordan%22%2C%22prod_name%22%3A%22AMD+Kaveri+A4-7300+APU+HD8470D+2-Core+2GB+500GB+CPU+Package%22%2C%22quantity%22%3A%221%22%2C%22branch%22%3A%22Dapitan%22%2C%22totalPrice%22%3A%2223500%22%7D%5D%7D&
模拟数据库操作并且正在工作
$content = '{"ORDERLIST":"{\"ORDER_LIST\":[{\"order_id\":\"1756\",\"uname\":\"jordan\",\"prod_name\":\"ABC\",\"quantity\":\"1\",\"branch\":\"Dapitan\",\"totalPrice\":\"23500\"},{\"order_id\":\"1756\",\"uname\":\"jordan\",\"prod_name\":\"AMD Kaveri A4-7300 APU HD8470D 2-Core 2GB 500GB CPU Package\",\"quantity\":\"1\",\"branch\":\"Dapitan\",\"totalPrice\":\"23500\"}]}"}';
$con = mysqli_connect("localhost","","","");
$response = json_decode($content, true);
$orderlist = json_decode($response['ORDERLIST'], true);
//$count = count($response);
for($x=0;$x<=count($response);$x++){
foreach($orderlist AS $row){
$id=$row[$x]['order_id'];
$uname=$row[$x]['uname'];
$query_parts[$x] = "("."'".$row[$x]['order_id']."'".","."'".$row[$x]['uname']."'".","."'".$row[$x]['prod_name']."'".","."'".$row[$x]['quantity']."'".","."'".$row[$x]['totalPrice']."'".")";
}
}
$sql_1 = "INSERT INTO `order` (`order_id`, `uname`) VALUES ('$id', '$uname')";
$sql_2 = "INSERT INTO orders (order_id, uname, prod_name, quantity, total_price) VALUES ".implode(',', $query_parts);
if ($con->query($sql_1) === TRUE) {
if ($con->query($sql_2) === TRUE) {
echo "New record created successfully";
} else {
echo "Error2: " . $sql_2 . "<br>" . $con->error;
}
} else {
echo "Error1: " . $sql_1 . "<br>" . $con->error;
}
答案 0 :(得分:0)
您要针对key
"tag"
发布数据,但在server side
上,您将从"ORDERLIST"
键$orderlist = json_decode($response['ORDERLIST'], true);
获取数据。在getParams
中移除参数并将完整的json或服务器端更改发送到$orderlist = json_decode($response['tag'], true);
希望这会有所帮助。