I am developing an android registration activity I'm using : Android Studio php postgreSQL
this is the piece of code which is supposed to insert in the database
public void insert_in_Database(final String first_name,final String last_name, final String username, final String password, final String email, final String phone) {
StringRequest strReq = new StringRequest(Request.Method.POST, Server.REGISTERATION_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Showing toast message of the response
try {
JSONObject object = new JSONObject(response);
String status= object.getString("status");
if (status.equalsIgnoreCase("success"))
{
Toast.makeText(SignUpActivity.this, status, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(SignUpActivity.this, status, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Showing toast
Toast.makeText(SignUpActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new Hashtable<>();
//Adding parameters
params.put(Server.KEY_USERNAME, username);
params.put(Server.KEY_PASSWORD, password);
params.put(Server.KEY_EMAIL, email);
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(strReq);
}
and this is registration.php
<?php
require_once(__DIR__.'/../dbConnect.php');
$success = array('status' => "success");
$failed_username = array('status' => "This username is already taken");
$failed_email = array('status' => "This email is already registered");
$failed = array('status' => "failed");
if($_SERVER['REQUEST_METHOD']=='POST'){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$query_username = "SELECT * FROM users WHERE username='$username'";
$result_username = pg_query($con, $query_username);
$rows_username = pg_num_rows($result_username);
if ($rows_username == 0)
{
$query_email = "SELECT * FROM users WHERE email='$email'";
$result_email = pg_query($con, $query_email);
$rows_email = pg_num_rows($result_email);
if($rows_email == 0){
$sql = "INSERT INTO users (id,first_name,last_name, username, password, email, phone)
VALUES(DEFAULT , '$first_name', '$last_name', '$username', '$password', '$email', '$phone')";
if(pg_query($con,$sql))
{
echo json_encode($success);
}
else{
echo json_encode($failed);
}
}else{
echo json_encode($failed_email);
}
}else
{
echo json_encode($failed_username);
}
pg_close($con);
}else{
echo json_encode($failed);
}
?>
I get this error
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
referring to the first and the last lines in the following block
StringRequest strReq = new StringRequest(Request.Method.POST, Server.REGISTERATION_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Showing toast message of the response
try {
JSONObject object = new JSONObject(response);
Can anyone help?