0

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

5
  • What type of characters is this returning? Commented Jul 8, 2015 at 19:18
  • Before // echoing JSON response , what do you get from print_r($response);? Is it the expected array, or null? Commented Jul 8, 2015 at 19:23
  • @ExecutiveCloser Hi, sorry I'm not sure what you mean, it isn't returning anything at all from "ntmContent" just prints "null" onto the webpage. Commented Jul 8, 2015 at 19:24
  • How are you retrieving the response on the front end? Jquery ajax call? Or is the above code right on the same webpage you're running in the browser? Commented Jul 8, 2015 at 19:26
  • Add junk data to see if it is returning correctly. $Data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip'); header('Content-type: text/javascript'); echo json_encode($Data); Commented Jul 8, 2015 at 19:28

1 Answer 1

1

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"]);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, this seems to have done the trick. I am now getting an output with the "odd" characters as unicode like this: locations: \u00a0Site\u00a0\u00a0\u00a0 is this the correct type of output I should expect?
What are the odd chars you mentioned in your question. What is the charset at the db
Well the \u00a0 is blank space but the "odd" chars are (I think) Ordinal indicators: º and Degree symbol: ° . The collation is utf8_general_ci if that helps. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.