Volley库W / System.err:org.json.JSONException:类型为java.lang.String的Value Connected无法转换为JSONObject

时间:2019-02-05 14:47:54

标签: java php android

我一直在尝试找出这个错误的根源,但找不到它。有谁可以帮助我追踪它?

谢谢家人

这是我的php代码

<?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');
}
?>

这是我的Android Studio代码

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);
}

我正在尝试找出错误的来源,但没有得到。 有帮助吗?

1 个答案:

答案 0 :(得分:0)

在您的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;
    }
}
?>