Reputation: 73
Hello I'm send an JSON object from android using volley library. I can not receive this JSON object in PHP. I checked by echo ING my JSON data I can see the object in my 'OnResponse Method'. It would be my pleaser if anyone can help me to solve it. I'll owe you a great debt. Here is my code ->
Android Volley Code ->
private void registerUser() {
JSONObject postObject = new JSONObject();
RequestQueue queue = Volley.newRequestQueue(this);
JSONObject historyObject = new JSONObject();
String url ="http://helpinghandbd.org/app/index.php";
try {
//historyObject.put("id","1");
historyObject.put("email","1234");
historyObject.put("password","1234");
postObject.put("user",historyObject);
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("LoginActivityJsonObject",""+postObject);
JsonObjectRequest objRequest = new JsonObjectRequest(Request.Method.POST, url,postObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("LoginActivity","OnResponse: "+response);
Toast.makeText(LoginActivity.this, String.valueOf(response), Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("OnError", String.valueOf(error.getMessage()));
}
});
queue.add(objRequest);
}
JSON Format is ->
{ 'user':{
'email':'1234',
'password':'1234'
}
}
And Finally PHP Code is ->
<?php
$data = file_get_contents("php://input");
//echo $data; -> //{ 'user':{'email':'1234','password':'1234'}};
$decode = json_decode($data,true);
$email = $decode->user['email'];
$password = $decode->user['passowrd'];
$servername = "localhost";
$username = "helpinghandbd_app";
$password = "Demopass000";
$dbname = "helpinghandbd_app";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$data = file_get_contents("php://input");
//{ 'user':{'email':'1234','password':'1234'}};
$sql = "INSERT INTO users (id,email,password)
VALUES (null, '$email', '$password')";
if ($conn->query($sql) === TRUE) {
echo $data;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
I can not receive JSON Object in PHP. Thanks in advance.
Upvotes: 0
Views: 941
Reputation: 5017
In your php
code, change
$decode = json_decode($data,true);
$email = $decode->user['email'];
$password = $decode->user['passowrd'];
to
$decode = json_decode($data,true);
$email = $decode['user']['email'];
$password = $decode['user']['passowrd'];
Upvotes: 1