排球请求中的UTF-8编码

Jen*_*sen 11 encoding android utf-8 android-volley

在我的Android应用程序中,我正在使用Volley加载json数据JsonArrayRequest.数据是由我自己创建的,我用Sublime用UTF-8编码保存它们.当我得到Response并填充我的时ListView,文本显示不正确(变音符号).这是我的请求的样子:

JsonArrayRequest request = new JsonArrayRequest(targetUrl,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(final JSONArray response) {
                        try {
                            fillList(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(request);
Run Code Online (Sandbox Code Playgroud)

当我使用此方法加载完全相同的数据时,所有文本都正确显示:

final StringBuilder builder = new StringBuilder();
        final HttpClient client = new DefaultHttpClient();
        final HttpGet httpGet = new HttpGet(request);
        try {
            final HttpResponse response = client.execute(httpGet);
            final StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                final HttpEntity entity = response.getEntity();
                final InputStream content = entity.getContent();
                final BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {

            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

所以对我来说,似乎我的json文件的编码没有问题.如何在Volley中修复此编码问题?

Mat*_*ape 23

如果你知道你要求的绝对所有文件都是UTF-8格式,听起来就是这样,那么你可能会考虑强制你的Volley请求返回UTF-8格式的字符串.您可以通过继承标准JSON请求来实现此目的.像这样的东西:

public class Utf8JsonRequest extends JsonRequest<JSONObject> {
    ...
    @Override
    protected Response<JSONObject> parseNetworkResponse (NetworkResponse response) {
        try {
            String utf8String = new String(response.data, "UTF-8");
            return Response.success(new JSONObject(utf8String), HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            // log error
            return Response.error(new ParseError(e));
        } catch (JSONException e) {
            // log error
            return Response.error(new ParseError(e));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Muh*_*cha 11

我有同样的问题,我使用UTF-8字符集解决它.

String str = "";
try {
     str = new String(strFromService.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {

 e.printStackTrace();
}

String decodedStr = Html.fromHtml(str).toString();
Run Code Online (Sandbox Code Playgroud)

我希望这对你有用


erl*_*man 8

不要在onResponse块中使用try {} catch,这在我的代码中给出了一些问题,而不是你可以像这样实现.

@Override 
onResponse(String s) {

s= fixEncoding(s);
Toast.makeToast(this,s,Toast.LENGTH_LONG).show();

}
Run Code Online (Sandbox Code Playgroud)

我想你会得到所需的结果

 public static String fixEncoding(String response) {
            try {
                byte[] u = response.toString().getBytes(
                        "ISO-8859-1");
                response = new String(u, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
            return response;
        }
Run Code Online (Sandbox Code Playgroud)


Kur*_*mer 5

这对我来说是一种魅力,我只是从@Muhammad Naeem的答案创建了一个静态方法,谢谢穆罕默德......

public static String fixEncodingUnicode(String response) {
    String str = "";
    try {
        str = new String(response.getBytes("ISO-8859-1"), "UTF-8");
    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
    }

    String decodedStr = Html.fromHtml(str).toString();
    return  decodedStr;
}
Run Code Online (Sandbox Code Playgroud)