Reputation: 1665
I am trying to do a user registration through my android app and data is stord in database using php. Everything works fine ie, data is inserted into database but cant see the JSON response in android.
Here is my code
Java
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
// Add your data
try {
jsonParams.put("user_name", "Steve Jobs");
jsonParams.put("user_email", "[email protected]");
jsonParams.put("user_password", "nopassword");
jsonParams.put("user_phone_number", "1234567890");
jsonParams.put("club_member_id", "24");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("POST", jsonParams.toString()));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
// Use UrlEncodedFormEntity to send in proper format which we need
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (JSONException e) {
}
// HttpClient httpclient = new DefaultHttpClient();
// HttpPost httppost = new HttpPost(url);
// ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("user_name", "Steve"));
// nameValuePairs.add(new BasicNameValuePair("user_email", "jjdnjd"));
// nameValuePairs.add(new BasicNameValuePair("user_password", "dcds"));
// nameValuePairs.add(new BasicNameValuePair("user_phone_number", "2343"));
// nameValuePairs.add(new BasicNameValuePair("club_member_id", "24"));
// Log.e("Params", String.valueOf(nameValuePairs.toString()));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// HttpResponse response = httpclient.execute(httppost);
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
Log.e("Data", String.valueOf(json));
PHP
$output=array();
$data = json_decode($_POST['POST'], true);
if(!empty($data)){
if(user_exists($data['user_email'])){
$result = db_conncet()->prepare("INSERT INTO `mir_users`(`name`, `password`, `email`, `phone_number`, `club_member_id`) VALUES (?,?,?,?,?)");
$data['club_member_id']=(isset($data['club_member_id']))?$data['club_member_id']:'NULL';
$temp=array(
$data['user_name'],
password_hash($data['user_password'],PASSWORD_BCRYPT),
$data['user_email'],
$data['user_phone_number'],
$data['club_member_id']
);
$result->execute($temp);
if($result->rowCount()){
$output=array(
'status'=>true,
'code'=>'103',
'message'=>'Registration success'
);
}else{
$output=array(
'status'=>false,
'code'=>'102',
'message'=>'Registration error'
);
}
}else{
$output=array(
'status'=>false,
'code'=>'102',
'message'=>'Email-ID Already taken'
);
}
echo json_encode($output);
}
Php script is working fine and the data is inserted into the database,but i can't see output of echo json_encode($output)
in my app.
Log.e("Data", String.valueOf(json));
getting null value in log.
I've checked the php error log i wll get this
PHP Notice: Undefined index: POST in /home/zama2n/public_html/miradmin/api/functions.php on line 21
i think this is the reason for getting empty json response in my app. But the insert query is working fine What is the problem .Please help me?
Upvotes: 4
Views: 418
Reputation: 11
Usually when u need to output json from php you need a header with a content type, might be what you are missing.
Header('Content-Type: application/json; charset: UTF-8');
echo json_encode($output);
die();
Upvotes: 0
Reputation: 11214
You have one url
but use it twice.
First with HttpClient you are posting some data but aren't doing anything with HttpResponse response = httpclient.execute(httppost);
After that you call the same url with a json parser. Without data.
All makes no sense.
Where are you talking about if you state that you cannot get a response?
`
Upvotes: 4