Android Login with php getting error after successful json parsing -
hi getting error if login correct user name , password.but json response correct showing user not found.where need change code. in advance. getting success json data if login correct user name , password else failure response getting every time showing user not found error.
login extends activity { button b; edittext et,pass; textview tv; @suppresswarnings("deprecation") httppost httppost; stringbuffer buffer; @suppresswarnings("deprecation") httpresponse response; @suppresswarnings("deprecation") httpclient httpclient; @suppresswarnings("deprecation") list<namevaluepair> namevaluepairs; progressdialog dialog = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.login); b = (button)findviewbyid(r.id.button01); et = (edittext)findviewbyid(r.id.username); pass= (edittext)findviewbyid(r.id.password); tv = (textview)findviewbyid(r.id.tv); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { dialog = progressdialog.show(login.this, "", "validating user...", true); new thread(new runnable() { public void run() { login(); } }).start(); } }); } @suppresswarnings("deprecation") void login(){ try{ httpclient=new defaulthttpclient(); httppost= new httppost("http://192.168.137.224/android_connect/get_product_details.php"); // make sure url correct. //add data namevaluepairs = new arraylist<namevaluepair>(2); // use same variable name posting i.e android side variable name , php side variable name should similar, namevaluepairs.add(new basicnamevaluepair("name",et.gettext().tostring().trim())); // $edittext_value = $_post['edittext_value']; namevaluepairs.add(new basicnamevaluepair("password",pass.gettext().tostring().trim())); httppost.setentity(new urlencodedformentity(namevaluepairs)); //execute http post request response=httpclient.execute(httppost); // edited james coderzheaven.. here.... responsehandler<string> responsehandler = new basicresponsehandler(); final string response = httpclient.execute(httppost, responsehandler); system.out.println("response : " + response); runonuithread(new runnable() { public void run() { // tv.settext("response php : " + response); dialog.dismiss(); } }); if(response.equalsignorecase("yes")) { runonuithread(new runnable() { public void run() { toast.maketext(login.this,"login success", toast.length_short).show(); } }); startactivity(new intent(login.this, otheractivty.class)); }else{ showalert(); } }catch(exception e){ dialog.dismiss(); system.out.println("exception : " + e.getmessage()); } } public void showalert(){ login.this.runonuithread(new runnable() { public void run() { alertdialog.builder builder = new alertdialog.builder(login.this); builder.settitle("login error."); builder.setmessage("user not found.") .setcancelable(false) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { } }); alertdialog alert = builder.create(); alert.show(); } }); } }
use http://www.java2s.com/code/jardownload/java-json/java-json.jar.zip library!
first of all, json not proper!
proper json: { "success": "yes", "message": "logged in" }
second use retrieve contents of "success" variable:
jsonobject myjson = new jsonobject(response); system.out.println(myjson.getstring("success"));
Comments
Post a Comment