php - How to send information from android app to server? -
hello everone started studying webservices in android , tried save information android app server
for firstly made php file. tried insert code using that. succesful in inserting code using php code file
<?php $con=mysqli_connect("localhost","root","","abhi_db"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } // escape variables security $uname = mysqli_real_escape_string($con, $_post['uname']); $email = mysqli_real_escape_string($con, $_post['email']); $sql="insert user_records (user_name, email) values ('$uname', '$email')"; if (!mysqli_query($con,$sql)) { die('error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); ?>
after tried send information app server hitting url. code used is
public class mainactivity extends activity { textview tv1; httpclient httpclient; httppost httppost; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv1 = (textview) findviewbyid(r.id.tv1); httpclient = new defaulthttpclient(); httppost = new httppost("http://10.0.2.2/name.php"); new getresult().execute(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public class getresult extends asynctask<void, void, void>{ string result = null; @override protected void doinbackground(void... arg0) { // todo auto-generated method stub try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("uname", "rocky")); namevaluepairs.add(new basicnamevaluepair("email", "rockss")); httppost.setentity(new urlencodedformentity(namevaluepairs)); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); log.e("exception", e.tostring()); } return null; } @override protected void onpostexecute(void result1) { // todo auto-generated method stub super.onpostexecute(result1); tv1.settext("data inserted"); //log.i("my response :: ", result); } } }
in textview message i.e data inserted problem value not inserted in database.
so if people kindly tell mistake making. somuch helpful me. in advance
you need execute http post request:
try { httpparams params = new basichttpparams(); params.setparameter(coreprotocolpnames.protocol_version, httpversion.http_1_1); httpclient httpclient = new defaulthttpclient(params); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("uname", "rocky")); namevaluepairs.add(new basicnamevaluepair("email", "rockss")); httppost.setentity(new urlencodedformentity(namevaluepairs, "utf-8")); string resp = httpclient.execute(httppost, new basicresponsehandler()); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); log.e("exception", e.tostring()); }
to data server use below code:
try { httpparams params = new basichttpparams(); params.setparameter(coreprotocolpnames.protocol_version, httpversion.http_1_1); httpclient httpclient = new defaulthttpclient(params); // put parameters if needed string resp = httpclient.execute(httppost, new basicresponsehandler()); string[] nameemailpairs = resp.split("<br>"); for(string pair : nameemailpairs){ string[] par = pair.split(" "); string name = par[0]; string email = par[1]; } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); log.e("exception", e.tostring()); }
Comments
Post a Comment