在凌空要求UTF-8编码 [英] UTF-8 encoding in Volley Requests

查看:175
本文介绍了在凌空要求UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我加载一个凌空 JsonArrayRequest JSON数据。该数据由自己创造,我救了他们与卓异使用UTF-8编码。当我得到的响应,并填写我的的ListView ,文本没有正确(变音)显示。这就是我的要求是这样的:

In my Android app I am loading json data with a Volley JsonArrayRequest. The data were created by myself and I saved them with Sublime with UTF-8 encoding. When I get the Response and fill my ListView, the texts are not displayed correctly (umlauts). This is what my Request looks like:

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);

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

When I load the exact same data with this method, all texts are displayed correctly:

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();
        }

所以对我来说,好像有一个与我的JSON文件的编码没有问题。我怎样才能在排球解决这个编码的问题?

So to me it seems like there is no problem with the encoding of my json file. How can I fix this encoding problem in Volley?

推荐答案

如果你知道,绝对都是您所请求将在UTF-8格式,这听起来像你这样的文件,那么你可以考虑强制您凌空请求返回UTF-8格式的字符串。你可以通过继承的标准JSON请求做到这一点。事情是这样的:

If you know that absolutely all of the files you are requesting will be in the UTF-8 format, which it sounds like you do, then you might consider forcing your Volley request to return UTF-8 formatted strings. You could accomplish this by subclassing the standard JSON request. Something like this:

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

这篇关于在凌空要求UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆