gpt4 book ai didi

php - Android Php Mysqli : Response cannot be parsed as JSON data

转载 作者:行者123 更新时间:2023-11-30 00:38:07 24 4
gpt4 key购买 nike

在我的应用程序中,当我想通过 php 文件从 Mysqli 数据库获取数据并且我无法获取数据“消息”时,Android logcat 返回“无法将响应解析为 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:请原谅我的英语,我是法国人。

最佳答案

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

而不是那个

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

有必要换成那个

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

因为Message字段在数据库中是text类型,Json无法直接对text进行编码。

关于php - Android Php Mysqli : Response cannot be parsed as JSON data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43064745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com