简体   繁体   中英

Volley library W/System.err: org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject

I have been trying yo figure out where this error lies but can't find it. Is there any ibe who can help me trace it?

thanks, family

Here is my php code

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $id = $_POST['id'];
    $password = $_POST['password'];

    require_once 'connect.php';

    $sql = "SELECT  * FROM students_table WHERE id = '$id'";

    $response = mysqli_query ($conn, $sql);

    $result = array();
    $result['login'] = array();

    if (mysqli_num_rows($response) === 1){

        $row =  mysqli_fetch_assoc($response);

        if(password_verify($password, $row['password'])){

            $index['id'] = $row['id'];
            $index['email'] = $row['email'];

            array_push($result['login'], $index);

            $result['success'] = "1";
            $result['message'] = "success";

            echo json_encode($result);

            mysqli_close($conn);

        }else {

            $result['success'] = "0";
            $result['message'] = "error";

            echo json_encode($result);

            mysqli_close($conn);
        }
    }

    header('Content-Type: application/json');
}
?>

Here is my Android studio Code

public void Login(final String idField, final String passwordField){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN, new Response.Listener<String>() {
        @Override
        public void onResponse(java.lang.String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String success = jsonObject.getString("success");
                JSONArray jsonArray = jsonObject.getJSONArray("login");

                Log.d("JSONR", jsonObject.toString());

                if (success.equals("1")){
                    for (int i = 0; i<jsonArray.length(); i++){
                        JSONObject object = jsonArray.getJSONObject(i);
                        String email = object.getString("email").trim();

                        Toast.makeText(LoginActivity.this, "Logged In. \n"+email, Toast.LENGTH_LONG).show();

                        //Access WelcomePage Page if credentials are authenticated
                        startActivity(new Intent(LoginActivity.this, WelcomePage.class));
                    }
                }
            }catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this, "Error" + e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(LoginActivity.this, "Error" + error.toString(), Toast.LENGTH_LONG).show();

                }
            })

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();

            params.put("id", idField);
            params.put("password", passwordField);
            return params;
        }
    };

    //manage network requests using the volley request queue. I'm creating a request queue and passing in request objects
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

I've beeb trying to figure out where the error is from days but do not get it. Any help?

try this in your php:

<?php require_once 'connect.php';

 //json response array
 $response = array("error" => FALSE);

if (isset($_POST['id']) && isset($_POST['password'])){ 

//receiving the post params
$id = mysqli_real_escape_string($conn,$_POST['id']);
$password = mysqli_real_escape_string($conn,$_POST['password']);

//get the student
$student = getStudent($id, $password);
if ($student != false){

    //user is found
    $response["error"] = FALSE;
    $response["student"] ["id"] = $student["id"];
    $response["student"] ["email"] = $student["email"];


    echo json_encode($response);
}

else {
    //user is not found with credentials
    $response["error"] =TRUE;
    $response["error_msg"] = "Wrong Student or Password";

    echo json_encode($response);
    }
}
else {

    //required post params is missing
    $response["error"] = TRUE;
    $response["error_msg"] = "Required paramaters missing";

    echo json_encode($response);

}

function getStudent($id, $password)
{
    global $conn;
    $query = "SELECT * from students_table where username = '{$id}'";
    $student = mysqli_query($conn, $query);
    $rows   = mysqli_num_rows($student);
    $row    = mysqli_fetch_assoc($student);

    if ($rows > 0 || password_verify($password, $row['password'])) {
        return $row;

    }
    else {
        return false;
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM