-2

我正在尝试在我的 android 应用程序中显示 utf-8 字符的阿姆哈拉语字符ListView,但出现的是一系列问号??????这是json输出的样子:

[{"ImageID":"1","ItemID":" Michael","ItemID_AM":" ???","ImagePath":"http:\/\/10.0.2.2
    \/android\/Pagination\/pics\/pic_a.png"},{"ImageID":"2","ItemID":" Mary","ItemID_AM":" 
    ????","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_b.png"},
    {"ImageID":"3","ItemID":"Sarah","ItemID_AM":"????","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_c.png"},
    {"ImageID":"4","ItemID":"John","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_d.png"},
    {"ImageID":"5","ItemID":"Paul","ItemID_AM":"???","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_e.png"},
    {"ImageID":"6","ItemID":"Martha","ItemID_AM":"??????","ImagePath":"http:\/\/10.0.2.2
    \/android\/Pagination\/pics\/pic_f.png"},
    {"ImageID":"7","ItemID":"Abby","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_a.png"},
    {"ImageID":"8","ItemID":"Bekei","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_b.png"},
    {"ImageID":"9","ItemID":"Nani","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_c.png"},
    {"ImageID":"10","ItemID":"Baby","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_d.png"},
    {"ImageID":"11","ItemID":"Made","ItemID_AM":"???","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_e.png"},
    {"ImageID":"12","ItemID":"Fuche","ItemID_AM":"??","ImagePath":"http:\/\/10.0.2.2\/android
    \/Pagination\/pics\/pic_f.png"},{"ImageID":"13","ItemID":"Michael Fulle","ItemID_AM":"???
     ??","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_a.png"},
    {"ImageID":"14","ItemID":" Mary Assefa","ItemID_AM":"???? ???","ImagePath":"http:\/
    \/10.0.2.2\/android\/Pagination\/pics\/pic_b.png"},{"ImageID":"15","ItemID":"Sarah 
Michael","ItemID_AM":"???? ???","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_c.png"},{"ImageID":"16","ItemID":"John Michael","ItemID_AM":"?? 
    ???","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_d.png"},
    {"ImageID":"17","ItemID":"Paul Michael","ItemID_AM":"??? ???","ImagePath":"http:\/
    \/10.0.2.2\/android\/Pagination\/pics\/pic_e.png"},{"ImageID":"18","ItemID":"Martha 
    Ephrem","ItemID_AM":"?????? ????","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics
    \/pic_f.png"},{"ImageID":"19","ItemID":"Item 19","ItemID_AM":"?? 19","ImagePath":"http:\/
    \/10.0.2.2\/android\/Pagination\/pics\/pic_h.png"},{"ImageID":"20","ItemID":"Item 
    20","ItemID_AM":"?? 20","ImagePath":""},{"ImageID":"21","ItemID":"Item 21","ItemID_AM":"??
     21","ImagePath":"http:\/\/10.0.2.2\/android\/Pagination\/pics\/pic_g.png"}]

ItemID_AM是我的数据库中存储阿姆哈拉语字符的字段名称,在我的数据库中它清楚地显示了阿姆哈拉语字符。哪个是Phonetic UNICODE.

这是mydatabase

    CREATE TABLE `images2` (
    `ImageID` int(2) NOT NULL auto_increment,
    `ItemID` varchar(50) NOT NULL,
    `ItemID_AM` varchar(50) NOT NULL,
    `ImagePath` varchar(50) NOT NULL,
    PRIMARY KEY (`ImageID`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=31 ;

    -- 
    -- Dumping data for table `images2`
    -- 

    INSERT INTO `images` VALUES (1, 'Michael', '???', 'http://10.0.2.2/android/Pagination/pics/pic_a.png');
    INSERT INTO `images` VALUES (2, 'Mary', '????', 'http://10.0.2.2/android/Pagination/pics/pic_b.png');
    INSERT INTO `images` VALUES (3, 'Sarah', '????', 'http://10.0.2.2/android/Pagination/pics/pic_c.png');
    INSERT INTO `images` VALUES (4, 'John', '??', 'http://10.0.2.2/android/Pagination/pics/pic_d.png');
    INSERT INTO `images` VALUES (5, 'Paul', '???', 'http://10.0.2.2/android/Pagination/pics/pic_e.png');
    INSERT INTO `images` VALUES (6, 'Martha', '??????', 'http://10.0.2.2/android/Pagination/pics/pic_f.png');

这是我的getAllData.php

<?php
$host = "localhost"; // host of MySQL server
$user = "root"; // MySQL user
$pwd = ""; // MySQL user's password
$db = "mydatabase"; // database name

header('content-type: application/json; charset=utf-8');

// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);

// Check connection
if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 

// query the application data
$sql = "SELECT * FROM images2 WHERE 1 ";
$result = mysqli_query($con, $sql);


// an array to save the application data
$rows = array();

// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $rows[] = $row; 
}

// close the database connection
mysqli_close($con);


// echo the application data in json format
echo json_encode($rows);
?>

我已经用过:header('content-type: application/json; charset=utf-8');但没有任何改变。有人可以请F1吗?

4

1 回答 1

3

您已经在Encoding JSON 中确认在 Android 应用程序中支持 UTF-8 字符,在常规浏览器中,您也会得到问号。这表明问题出在服务器端。

问题可能是从 PHP 到 MySQL 的数据库连接未设置为 UTF-8。在响应期间,任何非 ISO8895-1 字符都将转换为“?”。

在连接上设置字符集:

mysqli_set_charset($con, "utf8")

在您发出选择之前。

于 2013-10-15T13:21:28.487 回答