Android/PHP: how to POST/GET JSON data from Android to php -
android/php: how post/get json data android php?
currently stuck @ point sending jsonobject data php getting null values in response.
what want:
i sending 'username' , 'password' android in form of jsonobject , want retrieve same data in jsonobject in php response.
here code snippet
android_file.java
defaulthttpclient httpclient=new defaulthttpclient(); httppost httppost=new httppost(register_user); jsonobject jsonobject=new jsonobject(); httppost.setheader("accept", "application/json"); httppost.setheader("content-type", "application/json"); try { jsonobject.put("username",username.gettext().tostring()); jsonobject.put("password",password.gettext().tostring()); stringentity se=new stringentity("json="+jsonobject.tostring()); //httppost.addheader("content-type", "application/x-www-form-urlencoded"); se.setcontentencoding(new basicheader(http.content_type,"application/json")); httppost.setentity(se); string req=se.tostring(); httpresponse response=httpclient.execute(httppost); if(response!=null){ inputstream is=response.getentity().getcontent(); bufferedreader bufferedreader=new bufferedreader(new inputstreamreader(is)); stringbuilder sb=new stringbuilder(); string line=null; while((line=bufferedreader.readline())!=null){ sb.append(line+"\n"); } this.message=sb.tostring(); //toast.maketext(getbasecontext(), message, toast.length_long).show(); } } catch (jsonexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
login_check.php
<?php include('../core/init.php'); // $contents = file_get_contents('php://input'); // $contents = utf8_encode($contents); if($_post['json']) { $data=json_decode(stripslashes($_post['json'])); $username=$data['username']; $password=$data['password']; $response=array('username'=>$username,'password'=>$password); echo json_encode($response); } else{ $no_data=array('none'=>'no data received'); echo json_encode($no_data); } ?>
please let me know if doing wrong or missing point? in advance!
i dont know wrong code compared code mine working fine on diffrent apps , made changes code , hope works:
replace part:
jsonobject.put("username",username.gettext().tostring()); jsonobject.put("password",password.gettext().tostring()); stringentity se=new stringentity("json="+jsonobject.tostring()); se.setcontentencoding(new basicheader(http.content_type,"application/json")); httppost.setentity(se);
with:
list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("username", username.gettext().tostring())); params.add(new basicnamevaluepair("password",password.gettext().tostring())); httppost.setentity(new urlencodedformentity(params));
and web service (i think problem here):
<?php include('../core/init.php'); // $contents = file_get_contents('php://input'); // $contents = utf8_encode($contents); if(isset($_post['username']) && isset($_post['password'])) { $username=$_post['username']; $password=$_post['password']; $response = array(); $response['username'] = $username; $response['password'] = $password; echo json_encode($response); } else{ $no_data=array(); $no_data['none']= 'no data received'; echo json_encode($no_data); } ?>
please let me know if fixes problem.
Comments
Post a Comment