我有一个程序从MySQL数据库中获取字符串,使用相同的字符串创建SqlLite数据库,然后在Android应用程序ListView中显示它们。
问题是,当我从PHP方面得到回复时,它将立陶宛字符显示为“?”,但我使用
JSON_UNESCAPED_UNICODE
在json_encode函数中。问题在哪里?
我想提一下MySql unicode设置为“utf8_lithuanian_ci”
PHP文件:
include_once 'db_functions.php';
$db = new DB_Functions();
$users = $db->getMarsrutaiCount();
$a = array();
$b = array();
if ($users != false){
$no_of_users = mysqli_num_rows($users);
while ($row = mysqli_fetch_array($users)) {
$b["id"] = $row["id"];
$b["marsrutas"] = $row["marsrutas"];
$b["pasirinkimas_id"] = $row["pasirinkimas_id"];
array_push($a,$b);
}
echo json_encode($a, JSON_UNESCAPED_UNICODE);
}
Android方面的JSON响应:
ArrayList<HashMap<String, String>> usersynclist;
usersynclist = new ArrayList<HashMap<String, String>>();
// Create GSON object
Gson gson = new GsonBuilder().create();
try {
// Extract JSON array from the response
JSONArray arr = new JSONArray(response);
System.out.println(arr.length());
// If no of array elements is not zero
if(arr.length() != 0){
// Loop through each array element, get JSON object which has userid and username
for (int i = 0; i < arr.length(); i++) {
// Get JSON object
if(veiksmas == 1) {
JSONObject obj = (JSONObject) arr.get(i);
System.out.println(obj.get("id"));
System.out.println(obj.get("marsrutas"));
System.out.println(obj.get("pasirinkimas_id"));
// DB QueryValues Object to insert into SQLite
queryValues = new HashMap<String, String>();
// Add userID extracted from Object
queryValues.put("id", obj.get("id").toString());
// Add userName extracted from Object
queryValues.put("marsrutas", obj.get("marsrutas").toString());
queryValues.put("pasirinkimas_id", obj.get("pasirinkimas_id").toString());
// Insert User into SQLite DB
controller.insertMarsrutas(queryValues);