android json post data not being displayed/recieved in php -
i trying send data android app displayed in php page. according logs put in place, data send , has correct values on php page, displays empty array:
array()
this php file:
<?php if ($_server['request_method'] == 'post') { echo "post made"; } $json_received = $_post["json"]; $json_test = $_post["test"]; $json = file_get_contents('php://input'); $obj = json_decode($json_test); $obj1 = json_decode($json_received); $obj2 = json_decode($json); echo $obj1; echo $obj; echo $json; echo $json_test; echo $json_received; print_r($_post); ?>
java code:
public class confirmfragmenttab extends fragment implements onitemclicklistener, onitemlongclicklistener { public static final string arg_item_id = "sales_list"; listview salelistview; arraylist<salesreciepts> salesreciept; saleslistadapter saleslistadapter; productsdbhelper db, dp; activity activity; context context; button push; private string post_products = "http://bi.test.com/tests/products_api/post_products.php"; inputstream = null; private getemptask task; protected fragmentactivity mactivity; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); activity = getactivity(); context = getactivity(); db = new productsdbhelper(activity); } public void onattach(activity activity) { super.onattach(activity); mactivity = (fragmentactivity) activity; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.confirm_layout, container, false); findviewsbyid(rootview); task = new getemptask(activity); task.execute((void) null); salelistview.setonitemclicklistener(this); salelistview.setonitemlongclicklistener(this); globalapp.data().context = getactivity(); push.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //postlist(); new pushinvoice().execute(); } }); return rootview; } private void findviewsbyid(view view) { salelistview = (listview) view.findviewbyid(r.id.list_sale); push = (button) view.findviewbyid(r.id.sendrecords); } @override public void onresume() { super.onresume(); } @override public void onitemclick(adapterview<?> list, view arg1, int position, long arg3) { salesreciepts sale = (salesreciepts) list.getitematposition(position); if (sale != null) { bundle arguments = new bundle(); arguments.putparcelable("selectedsale", sale); customeditdialog customdialogfragment = new customeditdialog(); customdialogfragment.setarguments(arguments); customdialogfragment.show(getfragmentmanager(), customeditdialog.arg_item_id); } } @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long arg3) { salesreciepts sale = (salesreciepts) parent.getitematposition(position); // use asynctask delete database db.delete(sale); saleslistadapter.remove(sale); return true; } public class getemptask extends asynctask<void, void, arraylist<salesreciepts>> { private final weakreference<activity> activityweakref; public getemptask(activity context) { this.activityweakref = new weakreference<activity>(context); } @override protected arraylist<salesreciepts> doinbackground(void... arg0) { arraylist<salesreciepts> salelist = db.getcurrentsalesrecords(); return salelist; } @override protected void onpostexecute(arraylist<salesreciepts> slist) { if (activityweakref.get() != null && !activityweakref.get().isfinishing()) { log.d("sales", slist.tostring()); salesreciept = slist; if (slist != null) { if (slist.size() != 0) { saleslistadapter = new saleslistadapter(activity, slist); salelistview.setadapter(saleslistadapter); } else { toast.maketext(getactivity(), "no sales records", toast.length_long).show(); } } } } } private class pushinvoice extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... arg0) { //arraylist<recieptheader> invoicelist = db.getinvoiceheader(); arraylist<salesreciepts> entrylist = db.getsalesrecords(); list<namevaluepair> postvars = new arraylist<namevaluepair>(); postvars.add(new basicnamevaluepair("json", string.valueof(entrylist))); postvars.add(new basicnamevaluepair("test", "hello")); servicehandler jsonparser = new servicehandler(); try { httpclient httpclient = new defaulthttpclient(); // setup http post method passing url in case // database offline , ip address in case of localhost database httppost httppost = new httppost(post_products); //httppost httppost = new httppost("http://192.168.1.33/connect_to_new_table.php"); // pass on names of values inside post stringentity entity = new stringentity(postvars.tostring()); httppost.setentity(entity); httppost.setentity(new urlencodedformentity(postvars)); responsehandler<string> responsehandler = new basicresponsehandler(); final string response = httpclient.execute(httppost, responsehandler); = entity.getcontent(); if (response != null) log.e("sent", postvars.get(1).getvalue().tostring()); log.e("response", response); } catch (exception e) { log.e("error", e.tostring()); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); } } }
update: logs sent values. postvars name value list of values sent
08-19 10:19:22.194 1708-1725/com.example.prototype e/sent﹕ hello 08-19 10:19:22.194 1708-1725/com.example.prototype e/response﹕ post madejson=%5bsalesreciepts+%5bid%3d0%2c+product_description%3dbell+lager+500ml+ret+25x01+longneck%2c+qty%3d23%2c+unit%3d3658%2c+total%3d84134.0%5d%5d&test=hellohello[salesreciepts [id=0, product_description=bell lager 500ml ret 25x01 longneck, qty=23, unit=3658, total=84134.0]]array ( [json] => [salesreciepts [id=0, product_description=bell lager 500ml ret 25x01 longneck, qty=23, unit=3658, total=84134.0]] [test] => hello )
i did similar thing weeks ago , json data istruction:
$json = json_decode(file_get_contents('php://input'), true);
and json['namevar'] values.
you can find project on github if can helps you.
Comments
Post a Comment