【问题标题】:Encoding not correct with Volley JsonObjectRequest API - UTF-8Volley JsonObjectRequest API 编码不正确 - UTF-8
【发布时间】:2016-02-01 12:36:21
【问题描述】:

我的 API (nodejs) 和我的 android 客户端之间存在关于编码的问题。

Android Volley 请求:

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, tmpUrl, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.e("TEST", "" + response.toString());
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                    }
            );
queue.add(jsObjRequest);

我的 GET 请求的标头:

content-type → application/json; charset=utf-8

测试日志:

TEST: {"announces":[{"_id":"56360bca8c9356a3289788aa","title":"marché", ....

正常标题应该是“marché”,但它不起作用..

问题出在哪里? API 还是 Android 客户端?我在不同的项目中使用过这个 volley lib,一切正常......

【问题讨论】:

    标签: android json encoding utf-8 android-volley


    【解决方案1】:

    两者都不一定是错的,它们只是不匹配。 您的客户端假设结果的编码是 UTF-8,但显然不是。

    所以,您有两个选择 - 要么强制 nodejs 返回 UTF-8,要么找出它返回的编码,然后在 Android 端使用它。

    【讨论】:

    • 感谢您的回答。当我的朋友安装 apk(在另一部手机上)时,它可以正常工作。当它安装在我的手机或 android 模拟器上时,它就不能正常工作了..
    • 这真的不会改变答案。您的编码不匹配,因此您可能需要在双方(服务器和电话)上都明确,并在两个地方指定一个编码(例如 UTF-8)。让任何东西都采用默认编码可能会导致这种不匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2018-10-25
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多