i am using Volley library php mysql to build login system and i am facing this problem
Registration Error= 156 org.json.JSONException: Value Database of type java.lang.String cannot be converted to JSONObject
and when i check my database user details inserted successfully
i already tried these ways
<?php
$result = array();
$allValues = array();
$values = array();
if (mysqli_query($connection, $sql)) {
$values['status'] = 'true';
$values['message'] = 'successful';
} else {
$values['status'] = 'false';
$values['message'] = 'failed';
}
$allValues[] = $values;
$result['result'] = $allValues;
echo json_encode($result);
?>
change my java codes
JSONArray jsonArray =response.getJSONArray("result");
for(int i = 0 ; i<jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i("jsonsingleImage",jsonObject.toString());
String status = jsonObject.getString("status");
String message = jsonObject.getString("message");
JSONObject jo = new JSONObject(success.substring(1, success.length()-1));
new JSONObject(success.substring(success.indexOf("{"), success.lastIndexOf("}") + 1));
My Registration.php Codes
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
require_once("connection.php");
$first_name = $_POST['firstName'];
$first_name = strip_tags($first_name);
$first_name = ucfirst(strtolower($first_name)); // uppercase first letter
$first_name = str_replace(' ', '', $first_name); // remove spaces
$last_name = $_POST['lastName'];
$last_name = strip_tags($last_name);
$last_name = ucfirst(strtolower($last_name)); // uppercase first letter
$last_name = str_replace(' ', '', $last_name); // remove spaces
$email = $_POST['email'];
$email = strip_tags($email);
$email = str_replace(' ', '', $email); // remove spaces
$password = $_POST['password'];
$password = mysqli_real_escape_string($password);
$password = strip_tags($password);
$password = str_replace(' ', '', $password); // remove spaces
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$sql = "INSERT INTO users (firstName, lastName, email, password) VALUES ('$first_name', '$last_name', '$email', '$password')";
if (mysqli_query($connection, $sql)) {
$result['success'] = "True";
$result['message'] = "Successfully";
echo json_encode($result);
mysqli_close($connection);
} else {
$result['success'] = "False";
$result['message'] = "Failed";
echo json_encode($result);
mysqli_close($connection);
}
}
?>
SignActivity.java Codes
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLS.SIGNUP_API, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if (success.equals("True")) {
Toast.makeText(SignupActivity.this, "Registration successfully", Toast.LENGTH_LONG).show();
Log.i("Registration ======= ", "Registration successfully");
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(SignupActivity.this, "Registration Error= 156 " + e.toString(), Toast.LENGTH_LONG).show();
Log.i("Catch error 156 ======", e.toString());
}
}
},
'''