在Android中将json String转换为UTF-8

时间:2017-03-13 10:32:13

标签: android json

我想将服务器中的消息显示到我的应用中,我应该从json显示此消息。
但是消息文本如下:&#274 4;&#276 5;&#272 5;&#273 9;

我将此代码用于显示来自json的消息文本:

public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {

    ServerResponse resp = response.body();

    if (resp.getResult().equals("success")) {
        Snackbar.make(getView(), reps.getMessage(), Snackbar.LENGTH_LONG).show();

但在Snackbar中显示&#274 4;&#276 5;&#272 5;&#273 9; !!

我该如何解决?

2 个答案:

答案 0 :(得分:1)

您必须在显示字符串之前取消显示字符。您可以阅读this

总结一下。我建议你在你的项目中包含Apache commons-lang of Apache foundation in compile dependencies:

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

然后以这种方式修改代码:

import org.apache.commons.lang3.StringEscapeUtils;

...

public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
  ServerResponse resp = response.body();
  if (resp.getResult().equals("success")) {
    Snackbar.make(getView(), StringEscapeUtils.unescapeHtml4(reps.getMessage()), Snackbar.LENGTH_LONG).show();

答案 1 :(得分:0)

您可以尝试从响应消息中创建一个新的String对象。

String value = new String(reps.getMessage().getBytes("UTF-8"));

假设'reps.getMessage()'返回String