I have followed a tutorial from here http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ but I have come to a problem with the JSON response. It is returning null. I think it is due to the character encoding as some of the content that is being returned as null includes ° symbols.
The PHP code is:
// Check for empty result
if (mysqli_num_rows($result) > 0) {
// Looping through all results
$response["ntmNotices"] = array();
while ($row = mysqli_fetch_array($result)) {
$ntmRow = array();
$ntmRow["uploadDate"] = $row["uploadDate"];
$ntmRow["uploadTime"] = $row["uploadTime"];
$ntmRow["ntmTitle"] = $row["ntmTitle"];
$ntmRow["ntmDate"] = $row["ntmDate"];
$ntmRow["ntmContent"] = $row["ntmContent"];
// push single row into final response array
array_push($response["ntmNotices"], $ntmRow);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no rowsfound
$response["success"] = 0;
and the response is "ntmContent":null} ntmContent is what contains the odd characters but they appear fine in the database.
The tutorial doesn't cover issues surrounding character encoding so doesn't prepare for it but how should the $response be handled to accept the odd characters?
Thanks
When json_encode
fails to encode a value, it outputs null.
This is due to the odd characters you made reference too in your post.
json_encode()
only works with UTF-8
.
Try like this:
$ntmRow["ntmContent"] = utf8_encode($row["ntmContent"]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.