[英]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代码
<?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 这是我的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);
}
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:
<?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;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.