Send data from android app to PHP server -
here android code executes debugged , says data sent.
public void postregistrationdata(final string result) { // create new httpclient , post header thread thread = new thread(new runnable(){ @override public void run() { try { try { url url; httpurlconnection urlconn; url = new url ("http://192.168.*.**/server/mypage.php"); urlconn = (httpurlconnection)url.openconnection(); urlconn.setdoinput (true); urlconn.setdooutput (true); urlconn.setusecaches (false); urlconn.setrequestproperty("content-type","application/json"); urlconn.setrequestproperty("accept", "application/json"); urlconn.setrequestmethod("post"); urlconn.connect(); //create jsonobject here jsonobject jsonparam = new jsonobject(); jsonparam.put(result, true); // send post output. /*printout = new dataoutputstream(urlconn.getoutputstream ()); printout.writeutf(urlencoder.encode(jsonparam.tostring(),"utf-8")); log.i("notification", "data sent"); printout.flush (); printout.close ();*/ outputstreamwriter os = new outputstreamwriter(urlconn.getoutputstream(), "utf-8"); os.write(jsonparam.tostring()); log.i("notification", "data sent"); os.close(); } catch (malformedurlexception muex) { // todo auto-generated catch block muex.printstacktrace(); } catch (ioexception ioex){ ioex.printstacktrace(); } catch (jsonexception jex) { // todo auto-generated catch block jex.printstacktrace(); } } catch (exception e) { e.printstacktrace(); log.e("error", "there error in code"); } } }); thread.start(); }
here server(xampp) side php code inserts data correctly when run doesn't android app on phone. read similar responses in site , others solutions didn't work me last 2 days , decided see if can me. needed , appreciated.
<?php header('content-type: application/json'); // connect database $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "db"; $received = json_decode(stripslashes($_post['json'])); // if text data posted if(isset($received['result'])){ //print_r($_post); try { $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password); // set pdo error mode exception $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); //$data = urldecode($_post['regid']); $sql = "insert table (regid) values ('$received')"; // use exec() because no results returned $conn->exec($sql); echo "new record created successfully"; } catch (pdoexception $e) { echo $sql . "<br>" . $e->getmessage(); } $conn = null; }elseif ($received === null) { echo "not value passed!!!!!!!!!!!!"; } ?>
this logcat log.
04-14 21:08:44.464: d/dalvikvm(24167): gc_explicit freed 1544k, 27% free 30752k/42088k, paused 3ms+8ms, total 75ms 04-14 21:08:44.894: d/dalvikvm(24167): gc_explicit freed 1920k, 28% free 30464k/42088k, paused 5ms+7ms, total 71ms 04-14 21:08:45.234: d/dalvikvm(24167): gc_explicit freed 1667k, 28% free 30456k/42088k, paused 3ms+7ms, total 56ms 04-14 21:08:45.564: d/dalvikvm(24167): gc_explicit freed 1650k, 28% free 30455k/42088k, paused 4ms+6ms, total 57ms 04-14 21:08:45.984: d/dalvikvm(24167): gc_explicit freed 1654k, 28% free 30439k/42088k, paused 5ms+8ms, total 69ms 04-14 21:08:46.414: d/dalvikvm(24167): gc_explicit freed 1665k, 28% free 30441k/42088k, paused 4ms+6ms, total 68ms 04-14 21:08:46.764: d/dalvikvm(24167): gc_explicit freed 1638k, 28% free 30440k/42088k, paused 5ms+5ms, total 61ms 04-14 21:08:47.074: d/dalvikvm(24167): gc_explicit freed 1576k, 28% free 30454k/42088k, paused 3ms+6ms, total 56ms 04-14 21:08:47.504: d/dalvikvm(24167): gc_explicit freed 1661k, 28% free 30456k/42088k, paused 4ms+8ms, total 59ms 04-14 21:08:47.874: d/dalvikvm(24167): gc_explicit freed 1381k, 28% free 30439k/42088k, paused 4ms+6ms, total 58ms 04-14 21:08:48.224: d/dalvikvm(24167): gc_explicit freed 1542k, 28% free 30440k/42088k, paused 5ms+6ms, total 61ms 04-14 21:08:48.534: d/dalvikvm(24167): gc_explicit freed 1420k, 28% free 30454k/42088k, paused 5ms+6ms, total 55ms 04-14 21:08:48.904: d/dalvikvm(24167): gc_explicit freed 1516k, 28% free 30454k/42088k, paused 4ms+6ms, total 58ms 04-14 21:08:49.274: d/dalvikvm(24167): gc_explicit freed 1450k, 28% free 30438k/42088k, paused 3ms+6ms, total 69ms 04-14 21:08:49.564: d/dalvikvm(24167): gc_explicit freed 1376k, 28% free 30454k/42088k, paused 5ms+6ms, total 56ms 04-14 21:08:49.624: i/notification(24167): data sent 04-14 21:08:49.994: d/dalvikvm(24167): gc_explicit freed 1803k, 28% free 30468k/42088k, paused 5ms+6ms, total 68ms 04-14 21:08:50.344: d/dalvikvm(24167): gc_explicit freed 1442k, 28% free 30459k/42088k, paused 6ms+5ms, total 65ms 04-14 21:08:50.604: d/dalvikvm(24167): gc_explicit freed 1349k, 28% free 30444k/42088k, paused 3ms+5ms, total 57ms 04-14 21:08:50.834: d/dalvikvm(24167): gc_explicit freed 1207k, 28% free 30444k/42088k, paused 5ms+6ms, total 53ms 04-14 21:08:51.104: d/dalvikvm(24167): gc_explicit freed 1265k, 28% free 30449k/42088k, paused 3ms+6ms, total 68ms 04-14 21:08:51.374: d/dalvikvm(24167): gc_explicit freed 1326k, 28% free 30461k/42088k, paused 3ms+5ms, total 52ms 04-14 21:08:51.634: d/dalvikvm(24167): gc_explicit freed 1337k, 28% free 30458k/42088k, paused 3ms+5ms, total 51ms 04-14 21:08:51.864: d/dalvikvm(24167): gc_explicit freed 1224k, 28% free 30443k/42088k, paused 3ms+5ms, total 51ms 04-14 21:08:52.114: d/dalvikvm(24167): gc_explicit freed 1275k, 28% free 30458k/42088k, paused 10ms+6ms, total 60ms 04-14 21:08:52.354: d/dalvikvm(24167): gc_explicit freed 1267k, 28% free 30462k/42088k, paused 4ms+6ms, total 53ms 04-14 21:08:52.614: d/dalvikvm(24167): gc_explicit freed 1272k, 28% free 30459k/42088k, paused 3ms+6ms, total 52ms 04-14 21:08:52.824: d/dalvikvm(24167): gc_explicit freed 1219k, 28% free 30445k/42088k, paused 4ms+6ms, total 54ms 04-14 21:08:53.134: d/dalvikvm(24167): gc_explicit freed 1327k, 28% free 30461k/42088k, paused 5ms+13ms, total 86ms 04-14 21:08:53.394: d/dalvikvm(24167): gc_explicit freed 1278k, 28% free 30458k/42088k, paused 5ms+6ms, total 54ms 04-14 21:08:53.644: d/dalvikvm(24167): gc_explicit freed 1288k, 28% free 30443k/42088k, paused 3ms+5ms, total 51ms 04-14 21:08:53.884: d/dalvikvm(24167): gc_explicit freed 1206k, 28% free 30444k/42088k, paused 4ms+5ms, total 54ms 04-14 21:08:54.184: d/dalvikvm(24167): gc_explicit freed 1397k, 28% free 30443k/42088k, paused 3ms+6ms, total 51ms 04-14 21:08:54.414: d/dalvikvm(24167): gc_explicit freed 1208k, 28% free 30458k/42088k, paused 5ms+5ms, total 54ms 04-14 21:08:54.694: d/dalvikvm(24167): gc_explicit freed 1335k, 28% free 30458k/42088k, paused 3ms+6ms, total 52ms 04-14 21:08:54.894: d/dalvikvm(24167): gc_explicit freed 1219k, 28% free 30445k/42088k, paused 4ms+6ms, total 52ms 04-14 21:08:55.114: d/dalvikvm(24167): gc_explicit freed 1184k, 28% free 30448k/42088k, paused 4ms+8ms, total 64ms 04-14 21:08:55.334: d/dalvikvm(24167): gc_explicit freed 1210k, 28% free 30460k/42088k, paused 3ms+6ms, total 51ms 04-14 21:08:55.594: d/dalvikvm(24167): gc_explicit freed 1272k, 28% free 30460k/42088k, paused 3ms+6ms, total 54ms 04-14 21:08:55.874: d/dalvikvm(24167): gc_explicit freed 1334k, 28% free 30447k/42088k, paused 3ms+6ms, total 53ms 04-14 21:08:56.124: d/dalvikvm(24167): gc_explicit freed 1321k, 28% free 30448k/42088k, paused 4ms+5ms, total 53ms 04-14 21:08:56.384: d/dalvikvm(24167): gc_explicit freed 1280k, 28% free 30459k/42088k, paused 5ms+5ms, total 53ms 04-14 21:08:56.634: d/dalvikvm(24167): gc_explicit freed 1328k, 28% free 30460k/42088k, paused 3ms+6ms, total 56ms 04-14 21:08:56.834: d/dalvikvm(24167): gc_explicit freed 1162k, 28% free 30443k/42088k, paused 3ms+6ms, total 52ms 04-14 21:08:57.064: d/dalvikvm(24167): gc_explicit freed 1259k, 28% free 30445k/42088k, paused 3ms+6ms, total 53ms 04-14 21:08:57.354: d/dalvikvm(24167): gc_explicit freed 1296k, 28% free 30445k/42088k, paused 3ms+6ms, total 52ms 04-14 21:08:57.654: d/dalvikvm(24167): gc_explicit freed 1280k, 28% free 30458k/42088k, paused 3ms+6ms, total 55ms 04-14 21:08:57.964: d/dalvikvm(24167): gc_explicit freed 1333k, 28% free 30459k/42088k, paused 4ms+5ms, total 51ms 04-14 21:08:58.234: d/dalvikvm(24167): gc_explicit freed 1346k, 28% free 30447k/42088k, paused 8ms+6ms, total 58ms 04-14 21:08:58.484: d/dalvikvm(24167): gc_explicit freed 1268k, 28% free 30447k/42088k, paused 5ms+6ms, total 57ms 04-14 21:08:58.784: d/dalvikvm(24167): gc_explicit freed 1341k, 28% free 30458k/42088k, paused 3ms+5ms, total 55ms 04-14 21:08:59.034: d/dalvikvm(24167): gc_explicit freed 1272k, 28% free 30458k/42088k, paused 4ms+6ms, total 55ms 04-14 21:08:59.354: d/dalvikvm(24167): gc_explicit freed 1405k, 28% free 30445k/42088k, paused 4ms+6ms, total 55ms 04-14 21:08:59.604: d/dalvikvm(24167): gc_explicit freed 1257k, 28% free 30445k/42088k, paused 5ms+7ms, total 56ms 04-14 21:08:59.864: d/dalvikvm(24167): gc_explicit freed 1270k, 28% free 30460k/42088k, paused 4ms+7ms, total 58ms 04-14 21:09:00.184: d/dalvikvm(24167): gc_explicit freed 1396k, 28% free 30460k/42088k, paused 5ms+6ms, total 57ms 04-14 21:09:00.414: d/dalvikvm(24167): gc_explicit freed 1225k, 28% free 30443k/42088k, paused 5ms+6ms, total 61ms 04-14 21:09:00.654: d/dalvikvm(24167): gc_explicit freed 1272k, 28% free 30443k/42088k, paused 4ms+6ms, total 55ms 04-14 21:09:00.914: d/dalvikvm(24167): gc_explicit freed 1208k, 28% free 30443k/42088k, paused 4ms+6ms, total 53ms 04-14 21:09:01.184: d/dalvikvm(24167): gc_explicit freed 1239k, 28% free 30459k/42088k, paused 4ms+6ms, total 56ms 04-14 21:09:01.454: d/dalvikvm(24167): gc_explicit freed 1337k, 28% free 30458k/42088k, paused 3ms+5ms, total 51ms 04-14 21:09:01.684: d/dalvikvm(24167): gc_explicit freed 1223k, 28% free 30443k/42088k, paused 4ms+5ms, total 55ms 04-14 21:09:01.944: d/dalvikvm(24167): gc_explicit freed 1263k, 28% free 30444k/42088k, paused 5ms+5ms, total 54ms 04-14 21:09:02.204: d/dalvikvm(24167): gc_explicit freed 1330k, 28% free 30463k/42088k, paused 3ms+8ms, total 54ms 04-14 21:09:02.474: d/dalvikvm(24167): gc_explicit freed 1338k, 28% free 30461k/42088k, paused 4ms+6ms, total 67ms 04-14 21:09:02.734: d/dalvikvm(24167): gc_explicit freed 1274k, 28% free 30458k/42088k, paused 3ms+5ms, total 52ms 04-14 21:09:02.964: d/dalvikvm(24167): gc_explicit freed 1208k, 28% free 30458k/42088k, paused 4ms+5ms, total 52ms 04-14 21:09:03.224: d/dalvikvm(24167): gc_explicit freed 1266k, 28% free 30461k/42088k, paused 3ms+6ms, total 54ms 04-14 21:09:03.444: d/dalvikvm(24167): gc_explicit freed 1209k, 28% free 30475k/42088k, paused 4ms+5ms, total 54ms 04-14 21:09:03.724: d/dalvikvm(24167): gc_explicit freed 1322k, 28% free 30478k/42088k, paused 4ms+8ms, total 56ms
this may problem jsonparam.put(result, true); in code.
you entered result parameter key , boolean value , means everytime key different value, have send key set in php code.
i hope may worked you.
Comments
Post a Comment