【问题标题】:Volley library W/System.err: org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObjectVolley 库 W/System.err:org.json.JSONException:Java.lang.String 类型的连接值无法转换为 JSONObject
【发布时间】:2019-02-05 14:47:53
【问题描述】:

我一直在试图找出这个错误在哪里,但找不到。有没有ibe可以帮我查一下?

谢谢家人

这是我的 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 工作室代码

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

我一直在试图找出错误的来源,但没有得到它。 有什么帮助吗?

【问题讨论】:

  • 你能打印响应,看看那里的价值是什么???
  • 看不到任何响应,除了:2019-02-05 15:26:01.166 13106-13106/com.example.inductionbuddy W/System.err: org.json.JSONException: Value Connected
  • 因此响应包含一些 HTML 标记,例如显示
  • 是我的 php 文件中的错误吗? @Android 杀手
  • 是的,来自 PHP 的响应是错误的。它应该以 JSON 格式的字符串发送响应,以便它可以轻松地转换为 JSON 对象。但它是以html格式发送响应,它可能是一些错误或正确的响应,但它不应该是html格式。

标签: java php android


【解决方案1】:

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

【讨论】:

  • 这是执行代码后收到的错误:警告:无法修改标头信息 - 标头已发送(输出开始于 /storage/ssd5/461/8645461/public_html/connect/connect.php: 10) 在 /storage/ssd5/461/8645461/public_html/connect/connect.php 第 13 行 {"error":true,"error_msg":"Required paramaters missing"}
  • 尝试将 if ($_SERVER['REQUEST_METHOD'] == 'POST') 更改为 if (isset($_POST['id']) && isset($_POST['password']))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多