Send data from Android to Webservice to be inserted in my database -
i'm trying send few variables(or data) registration webservice android throught "httppost" method. here's code.
httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://mywebsitename.com/webservice/register.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(4); namevaluepairs.add(new basicnamevaluepair("fname", et3.gettext().tostring())); namevaluepairs.add(new basicnamevaluepair("lname", et4.gettext().tostring())); namevaluepairs.add(new basicnamevaluepair("userid", et5.gettext().tostring())); namevaluepairs.add(new basicnamevaluepair("pass", et6.gettext().tostring())); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block }
following register.php file code.
<?php $host = "localhost"; $user = "myusername"; $pass = "mypass"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<br>"); $fname = $_post['fname']; $lname = $_post['lname']; $userid = $_post['userid']; $pass = $_post['pass']; $db_select=mysql_select_db("the_db"); if(!$db_select){ die(mysql_error()); echo "error"; } $query= "insert users(fname, lname, userid, pass) values ('{$fname}', '{$lname}', '{$userid}', '{$pass}'); " ; if($medo=mysql_query($query)){ header("localhost/filename"); exit; }else{ echo"<p> error</p>"; die(mysql_error()); }
when try run it, gives me error. says on device "yourapp has stopped working". possibly nullpointer exception or that. don't know if correct way send simple variables names , passwords etc. register.php. can tell me problem here?
please have @ logcat output. should @ line in class got exception. post logcat output here if need help. recommend take @ android asynchronous http client. simplifies http communication. example of http post:
asynchttpclient client = new asynchttpclient(); requestparams rp = new requestparams(); rp.put("fname", et3.gettext().tostring()); rp.put("lname", et4.gettext().tostring()); rp.put("userid", et5.gettext().tostring()); rp.put("pass", et6.gettext().tostring()); client.post("http://mywebsitename.com/webservice/register.php", rp, new asynchttpresponsehandler() { @override public final void onsuccess(string response) { // handle response here } @override public void onfailure(throwable e, string response) { // went wrong } });
Comments
Post a Comment