php - Android JSONObject Post Volley using StringRequest -
i'am bit confuse have asked question related json decoding on php side. focus there on decoding jsonobject posted android volley using stringrequest. able create code concerning inserting data mysql , forgot i'm still having bad configuration on client side. i'm getting "e/volley: [215] basicnetwork.performrequest: unexpected response code 500" post code here both on android , php side. please guide me accordingly. tired here. hope me.
client side:
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(); (int = 0; < 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); } }
server side:
<?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 $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 `order` (`order_id`, `uname`) values ('$id', '$uname')"; $sql_2 = "insert 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"; }
removed statement first prevent 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"; }
i'm getting these bunch of code guest server side received 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&
simulated db operation , working
$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 $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 `order` (`order_id`, `uname`) values ('$id', '$uname')"; $sql_2 = "insert 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; }
you posting data against key
"tag"
on server side
fetching key "orderlist"
$orderlist = json_decode($response['orderlist'], true);
. either in getparams
remove param , send complete json or on server side change $orderlist = json_decode($response['tag'], true);
hope helps.
Comments
Post a Comment