【问题标题】:Android Php Mysqli : Response cannot be parsed as JSON dataAndroid Php Mysqli:无法将响应解析为 JSON 数据
【发布时间】:2017-08-21 05:50:07
【问题描述】:

在我的应用程序中,当我想通过 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();
        }
    }

我希望有人可以帮助我。感谢您的帮助。

Ps:对不起,我是英语,我是法国人。

【问题讨论】:

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

标签: php android json mysqli


【解决方案1】:

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

而不是那个

$alerts["Message"] = $row["Message"];

有必要用那个代替

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

因为“消息”字段在数据库中是文本类型,所以Json不能直接对文本进行编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多