0

I am working with a system which html code was set to Shift-JIS and mysql database was set to utf8_general_ci as following:

ãŠè¿”äº‹é ‚ã„ãŸã‚ˆã†ã§ã™ãŒ
返信文...

Now, I have developed a php and android code to transfer data from php to android. in php side,

<?php
require_once('dbConnect.php');
$user = $_POST['username'];
$sql = "SELECT * FROM `postcontents` WHERE id='$user' AND inside='0' order by date DESC";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('date'=> $row[6],
'name'=> $row[1],
'address'=> $row[2]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);

?>

However, Json-encode in php file consider the information which is in mysql as rubbish data. Also, Json-encode may change the formant of the data. Therefore, I cannot show Jappanies in Android or browser. The output is null.

{"date":"0000-00-00 00:00:00","name":null,"address":null}

How can I fix this problem?

By the way, I have tested

A problem with passing Japanese characters(UTF-8) via json_encode

difficulty passing Japanese characters(UTF-8) via json_encode

Android Java UTF-8 JSON

Community
  • 1
  • 1

2 Answers2

0

I had the same issue when trying to encode non-utf8 strings using php 5.3. Here is a comment on such behaviour: http://php.net/manual/en/function.json-encode.php#115733

You can test if you get utf8 strings from mysql via mb_detect_encoding() function.

Also you may want to ensure utf-8 for strings coming from mysql: How do I make MySQL return UTF-8? or convert encoding of your strings to utf8 via mb_convert_encoding() function.

Community
  • 1
  • 1
Oleja
  • 76
  • 6
0

I have solved it by using four suggestion from friends together

In php side:

mysql_query("SET NAMES 'shift_jis'", $con);

header("Content-type: application/json; charset=shift_jis");

function my_json_encode($arr)
{
        //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding
        array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'shift_jis'); });
        return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'shift_jis');

}

In android side

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "SHIFT_JIS"));