php - sending JSON to server using POST in android app -
i'm creating android app send json server-side php script , store in database
here's relevant code app
public string post(){ string url = "http://192.168.150.1/t2.php"; inputstream inputstream = null; string result = ""; seek { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); string json = ""; jsonobject jsonobject = new jsonobject(); jsonobject.put("str", "bang"); jsonobject.put("lat", 3.10); jsonobject.put("lon", 3.10); json = jsonobject.tostring(); toast.maketext(this, json, toast.length_long).show(); stringentity se = new stringentity(json); httppost.setentity(se); httppost.setheader("accept", "application/json"); httppost.setheader("content-type", "application/json"); httpresponse httpresponse = httpclient.execute(httppost); inputstream = httpresponse.getentity().getcontent(); if(inputstream != null) result = "success"; else result = "did not work!"; } grab (exception e) { log.d("inputstream", e.getlocalizedmessage()); } homecoming result; } and here's php script
<?php $connection = mysql_connect("192.168.150.1","****","****"); if (!$connection) { die("database connection failed: " . mysql_error()); } echo "connection success\n"; $db_select = mysql_select_db("ps1",$connection); if (!$db_select) { die("database selection failed: " . mysql_error()); } echo "db selections success"; ?> <html> <head> <title> hl</title> </head> <body> <p>hello</p> <?php $js = json_decode(file_get_contents('php://input')); //$js = json_decode($_post); ------------ ****** $atr =''; $val = ''; foreach ($js $a => $v) { $atr = $atr.','.$a; $val = $val.','.$v; } $atr = ltrim($atr,','); $val = ltrim($val,','); echo "insert js (".$atr.") values (" .$val . ");"; $result = mysql_query("insert js (".$atr.") values (" .$val . ");", $connection); if (!$result) { die("database query failed: " . mysql_error()); } ?> </body> </html> <?php mysql_close($connection); ?> now if seek send json object app, nil happens
but if try
curl -h "content-type: application/json" -d '{"str":"\"hello\"","lat":"3.1","lon":"5.2"}' localhost/t2.php
from command-line works.
also if seek uncomment $js = json_decode($_post); line in php script , comment out php://input line, json object not transferred values "", 0,0 inserted database
i think there must error app code.i'm next tutorial , code here http://hmkcode.com/android-send-json-data-to-server/
questions
can explain i'm doing wrong , plausible solution? what's difference between php://input , $_post?thanks
quest
use method create post request :
public string makepostrequest(string url, list<namevaluepair> namevaluepairs) { string response = ""; seek { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); response = entityutils.tostring(httpentity); } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } log.d(logtag, "post response >>> " + response); homecoming response; } usage :
in java :
list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("json",jsonobject.tostring())); string response = makepostrequest(string url, namevaluepairs ); server side php :
$jsoninput = $_post['json']; json_decode($jsoninput); php android json curl
No comments:
Post a Comment