android / java - 从服务器(json)接收utf-8字符串

时间:2014-07-20 16:38:19

标签: android android-activity

我正在尝试通过json编码函数从php脚本接收utf-8中的字符串。该字符串例如是:\ u00e9 \ u00e5 \ u00f1 \ u00e9。 (我认为php脚本做得很好,看起来很正常utf-8)

android将此字符串显示为奇怪的字母。我试图转换字符串,但它不起作用。字符串是“name” - 它是希伯来语中的变量。

    JSONObject json = new JSONObject(data[x]);
name = json.getString("name");
weight  = json.getString("weight");
height  = json.getString("height"); 
target  = json.getString("target");
calories = json.getDouble("calories");
bmi = json.getDouble("bmi");
bmr = json.getDouble("bmr");

String name2 = null;

  byte[] bytes = name.getBytes();
 name2 = new String(bytes, "UTF-8");

    Toast.makeText(getApplicationContext(),"n" + name2, Toast.LENGTH_SHORT).show();

php脚本:

     // loading values from database...

     $name = utf8_encode($name);
    $data = array(name => "$name",pass => "$pass");
    $length=count($array);

    echo json_encode($data); 

我也试过了:

String name2 = null;
    byte[] bute = null;
    bute = name.getBytes();
    try {
         name2= new String(bute, "UTF-8");


    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

0 个答案:

没有答案