Encoding in Android TextView

2019-02-09 11:39发布

I'm new in Android.
When using Json Parsing, I have a problem with display text(maybe cause by font, I don't know).
This is my Json return:

{"Response":[{"Id":829,"Name":"Tiền không đem lại hạnh phúc nhưng...","ShortDescription":"Một tỷ phú tâm sự với bạn,...  

But when I parse in TextView in android, the "Name" become:

 "Ti�n không đem lại hạnh phúc nhưng..."

This text is in Vietnamese. How can I fix it?

3条回答
手持菜刀,她持情操
2楼-- · 2019-02-09 12:12

I fixed it. My problem is UTF-8 charset.

String name = "";
try {
    name = new String(c.getString("NAME").getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {

    e.printStackTrace();
}

String decodedName = Html.fromHtml(name).toString();
查看更多
虎瘦雄心在
3楼-- · 2019-02-09 12:14

Use a webView to display text like this:

myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);
查看更多
劳资没心,怎么记你
4楼-- · 2019-02-09 12:24

The problem you are getting actually encoding. So the json string you are getting from your response need to add a proper encoding format. So just do like this.

Whlie posting a json use like this

StringEntity strEntity = new StringEntity(Your_json_string, HTTP.UTF_8);

While getting the json object use like this

    if (statuscode == HttpStatus.SC_OK) {
        String responseStr = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
查看更多
登录 后发表回答