Php Android:JSONException在尝试从服务器检索数据时被捕获

Php Android:JSONException在尝试从服务器检索数据时被捕获,php,android,json,Php,Android,Json,我现在正在编写一段代码,从服务器检索JSON数组。对于Android方面,我使用的是Okhttp3API,下面是我的部分代码 @Override public void onResponse(Call call, Response response) throws IOException { String strResponse = response.body().string();

我现在正在编写一段代码,从服务器检索JSON数组。对于Android方面,我使用的是Okhttp3API,下面是我的部分代码

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String strResponse = response.body().string();
                Log.i(TAG, strResponse);
                try {
                    JSONObject jsonObject = new JSONObject(strResponse);
                    boolean error = jsonObject.getBoolean("error");

                    if(!error) {
                        String uid = jsonObject.getString("uid");
                        JSONArray people = jsonObject.getJSONArray("users");
                        final String name = people.getJSONObject(0).getString("name");

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "it works! " + name, Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else {
                        final String strError = jsonObject.getString("error_msg");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), strError, Toast.LENGTH_SHORT).show();
                            }
                        });
                        hideDialog();
                    }
                } catch (final JSONException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.i(TAG, "JSONException caught: " + e.getMessage());
                            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                } finally {
                    hideDialog();
                }
            }
        });
这是上面代码连接到的PHP文件

<?php

// when you get a post request with a name called 'search_name'
if(isset($_POST['search_name'])) {

    // get the name value
    $search_name = $_POST['search_name'];

    require_once 'include/db_connect.php';
    $db = new DB_Connect();
    $conn = $db->connect();

    $users = array("error" => FALSE);
    $stmt = $conn->prepare("SELECT unique_id, name, email from users where name LIKE '%$search_name%'");
    $stmt->bind_param("ss", $name, $email);
    $stmt->execute();
    $stmt->store_result();

    // if user with the queried name exists
    if($stmt->num_rows > 0) {
        while($row = mysqli_fetch_array($stmt, MYSQL_ASSOC)) {
            $row_array["uid"] = $row["unique_id"];
            $row_array["users"]["name"] = $row['name'];
            $row_array["users"]["email"] = $row['email'];
            array_push($users, $row_array);
        }
        echo json_encode($users);
    } else {
        $stmt->close();
        $response["error"] = TRUE;
        $response["error_msg"] = "No matching users found.";
        echo json_encode($response);
    }
} 

// if the post request is not what this file is supposed to work with
else {
    $response["error"] = TRUE;
    $response["error_msg"] = "An error occurred while processing your request. Please try again later.";
    echo json_encode($response);
}
?>
根据
日志
,我现在开始相信问题是由于PHP文件中的
bind_param()
函数中的参数错误造成的。那我该怎么修呢?

试试这个

$stmt = $conn->prepare("SELECT unique_id, name, email from users where name LIKE :name");
$stmt->bind_param(":name", '%' . str_replace('%', '\%', $name) . '%', PDO::PARAM_STR);
如果您想检查数据库中的数据是否符合预期。转储它:

var_dump($response);

请发布错误日志或异常堆栈跟踪?您的问题似乎在这里:比如“%$name%”“”-->它应该类似于:name和
%
通配符应该在变量内。e、 g.“%”$名称.“%”我在
phpMyAdmin
上测试了该语句,它似乎工作正常。
JSONException在尝试从服务器检索数据时被捕获。不,那不是。数据从服务器中检索正常。尝试从数据生成json对象时会出现异常。你最好先检查一下数据是否有效。@greenapps你能给我一些解决方案吗?今天我花了好几个小时来处理这个问题,但我还是没能说到点子上。。
var_dump($response);