Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Php Mysqli:无法将响应解析为JSON数据_Php_Android_Json_Mysqli - Fatal编程技术网

Android Php Mysqli:无法将响应解析为JSON数据

Android Php Mysqli:无法将响应解析为JSON数据,php,android,json,mysqli,Php,Android,Json,Mysqli,在我的应用程序中,当我想通过php文件从Mysqli数据库获取数据,但无法获取数据“消息”时,logcat Android会返回“响应无法解析为JSON数据” PHP文件: <?php /* * Following code will get all cars * A alert is identified by id */ ini_set('error_reporting', E_ALL); ini_set('display_errors', '1'); // array for JS

在我的应用程序中,当我想通过php文件从Mysqli数据库获取数据,但无法获取数据“消息”时,logcat Android会返回“响应无法解析为JSON数据”

PHP文件:

<?php

/*
* Following code will get all cars
* A alert is identified by id
*/
ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');
// array for JSON response
$response = array();


// include db connect class
require_once('/***/db_config/db_connect.php');

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["id"])) {
    $id = $_GET['id'];

// get list of car from Vehicule table
    $result = mysqli_query($db->connect(),
    "SELECT Alerte.id, Alerte.DateCreation ,SousAlerte, Recepteur, Utilisateur.pseudo as lanceur, Message.Message, Alerte.Lanceur as idLanceur FROM Alerte, Message,Utilisateur  WHERE Recepteur= $id and Alerte.Message = Message.id and Alerte.Lanceur=Utilisateur.id");

    if (!empty($result)) {
        // check for empty result
        if (mysqli_num_rows($result) > 0) {


            // user node
            $response["alerts"] = array();

            while ($row = mysqli_fetch_array($result)) {

                $alerts = array();
                $alerts["id"] = $row["id"];
                $alerts["SousAlerte"] = $row['SousAlerte'];
                $alerts["Message"] = $row["Message"];
                $alerts["idLanceur"] = $row['idLanceur'];
                $alerts["Lanceur"] = $row['lanceur'];
                $alerts["Recepteur"] = $row['Recepteur'];
                $alerts["DateCreation"] = $row['DateCreation'];

                array_push($response["alerts"], $alerts);

            }

            // success
            $response["success"] = 1;

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no alert found
            $response["success"] = 0;
            $response["message"] = "No alert found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no alert found
        $response["success"] = 0;
        $response["message"] = "No alert found";

        // echo no users JSON
        echo json_encode($response);
    }

} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
    // echoing JSON response
    echo json_encode($response);
}
?>

我从JAVA方法中获取数据:

 public List<AlertData> getAllAlerts() {
        return allAlerts;
    }

    public void setAllAlerts(JSONObject jsonObject) {
        try {
            JSONArray alerts = jsonObject.getJSONArray("alerts");
            for (int i = 0; i < alerts.length(); i++) {
                JSONObject row = alerts.getJSONObject(i);

                String id = row.getString("id");
               String message = row.getString("Message");
                String lanceur =row.getString("lanceur");
                String sousAlerte = row.getString("SousAlerte");
                String idLanceur = row.getString("idLanceur");
                String idRecepteur = row.getString("Recepteur");
                String dateCreation = row.getString("DateCreation");
                AlertData alert = new AlertData(id, message, sousAlerte,lanceur, idLanceur, idRecepteur, dateCreation);
                allAlerts.add(alert);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
公共列表getAllAllerts(){ 归还所有货物; } public void setAllAlerts(JSONObject JSONObject){ 试一试{ JSONArray警报=jsonObject.getJSONArray(“警报”); 对于(int i=0;i 我希望有人能帮助我。谢谢你的帮助

请原谅我的英语,我是法国人。

我解决了我的问题。 添加方法utf8_encode()就足够了

相反

$alerts["Message"] = $row["Message"];
有必要用这个来代替

$alerts["Message"] = utf8_encode($row["Message"]);

因为字段“Message”是数据库中的文本类型,因此Json不能直接编码文本。

setAllAlerts
方法中打印
jsonObject
时,输出是什么?在我的方法中,jsonObject不返回任何内容