Monday, 15 April 2013

json - Sending data from Android to php webservice to validate Login -



json - Sending data from Android to php webservice to validate Login -

i have login.php script validate username , password entered in android. code below

<?php include('dbconnect.php'); $data=file_get_contents('php://input'); $json = json_decode($data); $tablename = "users"; //username , password sent android $username=$json->{'username'}; $password=$json->{'password'}; //protecting mysql injection $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $password = md5($password); $sql = "select id $tablename u_username='$username' , password='$password'"; //querying database $result=mysql_query($sql); //if found, number of rows must 1 if((mysql_num_rows($result))==1){ //creating session session_register("$username"); session_register("$password"); print "success"; }else{ print "incorrect details"; } ?>

i have android class user come in username , password. code below.

public class loginactivity extends activity { public static final string loginuri="http://.../login.php"; @override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.login); buttonsubmit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string userid = ""; userid=login(edittextusername.gettext().tostring(), edittextpassword.gettext().tostring()); if (edittextpassword.gettext().tostring() != null & edittextusername.gettext().tostring() != null){ //used move cases activity intent casesactivity = new intent(getapplicationcontext(), casesactivity.class); startactivity(casesactivity); casesactivity.putextra("username", userid); } else{ //display toaster error toast.maketext(getapplicationcontext(),"this error message", toast.length_long).show(); } } }); private string login(string username, string password){ jsonobject jsonobject = new jsonobject(); string success = ""; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(loginuri); httpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams,10000); httpconnectionparams.setsotimeout(httpparams,10000); seek { jsonobject.put("username", username); log.i("username", jsonobject.tostring()); jsonobject.put("password", password); log.i("password", jsonobject.tostring()); stringentity stringentity = new stringentity(jsonobject.tostring()); stringentity.setcontentencoding(new basicheader(http.content_type, "application/json")); httppost.setentity(stringentity); httpresponse httpresponse = httpclient.execute(httppost); httpentity entity = httpresponse.getentity(); if (entity != null) { success = entityutils.tostring(httpresponse.getentity()); log.i("success", success); } }catch (ioexception e){ log.e("login_issue", e.tostring()); }catch (jsonexception e) { e.printstacktrace(); } homecoming success; } }

i next error: error/androidruntime(29611): fatal exception: main android.os.networkonmainthreadexception. understand need thread , thinking of using asynctask, not know set in class.

could give me advice in using json sending , receiving info android.

thank help,

you can alter code using asynctask calling login method within doinbackground , start next activity on onpostexecute when login successful :

private class loginoperation extends asynctask<string, void, string> { string str_username=; string str_password=; public loginoperation(string str_username,string str_password){ this.str_password= str_password; this.str_username= str_username; } @override protected void onpreexecute() { // show progress bar here } @override protected string doinbackground(string... params) { // phone call login method here string userid=login(str_username,str_password); homecoming userid; } @override protected void onpostexecute(string result) { // start next activity here if(result !=null){ intent casesactivity = new intent(getapplicationcontext(), casesactivity.class); casesactivity.putextra("username", result); your_activiy.this.startactivity(casesactivity); } }

and start loginoperation asynctask on button click as:

buttonsubmit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if (edittextpassword.gettext().tostring() != null & edittextusername.gettext().tostring() != null){ // start asynctask here new loginoperation(edittextusername.gettext().tostring(), edittextpassword.gettext().tostring()).execute(""); } else{ // code here } } }); }

android json web-services android-asynctask

No comments:

Post a Comment