Posting JSON from PHP to Android issue -
Posting JSON from PHP to Android issue -
hello having issue sending json object php android device. have done before person , worked fine not seem work.
it simple login script. should homecoming user's info or "false" based on whether login email , password entered correct.
the response visible when tried in browser according colleague, gets empty array when viewing on android development machine.
this php code:
<?php include('config.php'); include('functions.php'); include('password_hash_lib/password.php'); if (!isset($_request["device"])) { $email = $_post['email']; $password = $_post['password']; seek { if (authenticate($email, $password)) { echo "true"; } else { echo "false"; } } grab (exception $e) { echo $e->getmessage(); } } else if (isset($_request["device"])) { $device = $_request['device']; $email = $_request['email']; $password = $_request['password']; if ($device == 'mobi') { seek { if (authenticate($email, $password)) { $curruser = explode("+", $_session['sess_user_auth']); $arr = new arrayobject(array(), arrayobject::std_prop_list); $arr->userid = $curruser[0]; $arr->email = $curruser[1]; $arr->fullname = $curruser[2]; $arr->displaypic = $curruser[3]; $arr->displayname = $curruser[4]; echo str_replace("\\", "", json_encode((object) $arr)); } else { echo "false"; } } grab (exception $e) { echo $e->getmessage(); } } } ?>
this android code colleague wrote:
@override protected boolean doinbackground(void... params) { // todo auto-generated method stub boolean status=false; loginmgr lmgr=new loginmgr(getsherlockactivity().getbasecontext()); seek { httpclient httpclient= new defaulthttpclient(); httpget httpget=new httpget("http://www.xxxxx.com/login.php?device=mobi&email=xxxxxx@gmail.com&password=xxxxxxx "); httpresponse response= httpclient.execute(httpget); int statuscode=response.getstatusline().getstatuscode(); if(statuscode==200){ inputstream is=response.getentity().getcontent(); bufferedreader br=new bufferedreader(new inputstreamreader(is)); final stringbuilder stringbuild= new stringbuilder(); string curr_line; if((curr_line=br.readline())!=null){ stringbuild.append(curr_line); string jsonstr=stringbuild.tostring(); getsherlockactivity().runonuithread(new runnable() { @override public void run() { // todo auto-generated method stub toast.maketext(getsherlockactivity(), stringbuild.tostring(), toast.length_long).show(); } }); if(!jsonstr.equalsignorecase("false")){ jsonobject job=new jsonobject(jsonstr); string str=job.getstring("3").replace("\\/","/"); string image="http://www.xxxxx.com/img/timthumb.php?&h=50&w=50&src= "+str; lmgr.loginuser(new login(job.getstring("0"),job.getstring("4"),job.getstring("1"),image)); status=true; jsonobject job=new jsonobject(jsonstr); if(job.getstring("success").equalsignorecase("1")){ lmgr.loginuser(new login(job.getstring("userid"),job.getstring("username"),job.getstring("useremail"),job.getstring("userimage"))); status=true; } } } } grab (clientprotocolexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (jsonexception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming status; }
i have tried using homecoming instead of echo in code still did not work. there no errors empty array.
this should solve problem. seek consistent in script returns. must returns same kind of info (json
) if fails.
<?php include('config.php'); include('functions.php'); include('password_hash_lib/password.php'); if (!isset($_request["device"])) { $email = $_post['email']; $password = $_post['password']; seek { if (authenticate($email, $password)) echo json_encode(array('auth' => true)); else echo json_encode(array('auth' => false)); } grab (exception $e) { echo json_encode(array('error' => $e->getmessage())); } } else if (isset($_request["device"])) { $device = $_request['device']; $email = $_request['email']; $password = $_request['password']; if ($device == 'mobi') { seek { if (authenticate($email, $password)) { $curruser = explode("+", $_session['sess_user_auth']); $json = array(); $json['userid'] = $curruser[0]; $json['email'] = $curruser[1]; $json['fullname'] = $curruser[2]; $json['displaypic'] = $curruser[3]; $json['displayname'] = $curruser[4]; echo json_encode($json); } else { echo json_encode(array('auth' => false)); } } grab (exception $e) { echo json_encode(array('error' => $e->getmessage())); } } }
edit: based on lastly update, you've got mistakes in android code. i've tried improve haven't tested it.
@override protected boolean doinbackground(void... params) { boolean status = false; loginmgr lmgr = new loginmgr(getsherlockactivity().getbasecontext()); seek { // connect remote server httpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget("http://www.xxxxx.com/login.php?device=mobi&email=xxxxxx@gmail.com&password=xxxxxxx"); httpresponse response = httpclient.execute(httpget); int statuscode=response.getstatusline().getstatuscode(); if(statuscode==200) { // read response inputstream = response.getentity().getcontent(); bufferedreader br = new bufferedreader(new inputstreamreader(is)); final stringbuilder stringbuild= new stringbuilder(); string curr_line = null; while ((curr_line = br.readline()) != null) { stringbuild.append(curr_line); } getsherlockactivity().runonuithread(new runnable() { @override public void run() { toast.maketext(getsherlockactivity(), stringbuild.tostring(), toast.length_long).show(); } }); // parse response json jsonobject json = new jsonobject( stringbuild.tostring() ); if (json.has("auth")) { if (json.getboolean("auth")) { // authentificate success getsherlockactivity().runonuithread(new runnable() { @override public void run() { toast.maketext(getsherlockactivity(), "success", toast.length_long).show(); } }); } else { // in case, authentification has failed getsherlockactivity().runonuithread(new runnable() { @override public void run() { toast.maketext(getsherlockactivity(), "wrong user credentials", toast.length_long).show(); } }); } } else if (json.has("userid")) { string userid = json.getstring("userid"); string email = json.getstring("email"); string fullname = json.getstring("fullname"); string displaypic = json.getstring("displaypic").replace("\\/","/"); string displayname = json.getstring("displayname"); // in case, got info user string image = "http://www.xxxxx.com/img/timthumb.php?&h=50&w=50&src=" + displaypic; lmgr.loginuser(new login(json.getstring("0"), json.getstring("4"), json.getstring("1"), image)); status = true; jsonobject job = new jsonobject(jsonstr); if(job.getstring("success").equalsignorecase("1")) { lmgr.loginuser(new login(userid, fullname, email, displaypic)); status = true; } } } } grab (exception e) { e.printstacktrace(); } homecoming status; }
php android json
Comments
Post a Comment