从Android App调用PHP REST API不会正确显示变音符号(äüö)

问题描述 投票:0回答:1

我在PHP中构建了一个简单的REST API,它向我的MySQL数据库发出查询(完全是utf-8编码)并将结果作为JSON数组返回。在PHP中,我还将字符集设置为UTF-8:mysqli_set_charset($con, "UTF8");

因此,当我在网络浏览器中调用http://<host>/getData.php这样的服务时,我会得到这样的结果:

[{"id":"6","name":"föö","content":"bär"}]

这是完全正确的。

另外,我使用OkHttp3从我的Android应用程序中调用我的REST服务。但这总是给我以下输出:

[{"id":"6","name":"f&ouml;&ouml;","content":"b&auml;r"}]

显然,charset或类似的东西有问题。

这是我的JAVA代码:

OkHttpClient client = new OkHttpClient();

    HttpUrl.Builder urlBuilder = HttpUrl.parse(config.getServerRoot() + php).newBuilder();

    String url = urlBuilder.build().toString();

    Request request = new Request.Builder()
            .url(url)
            .build();

    // Get a handler that can be used to post to the main thread
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, final Response response) throws IOException {
            // Check for failure
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // Read data on the worker thread
            final String result = response.body().string();

            // result = [{"id":"6","name":"f&ouml;&ouml;","content":"b&auml;r"}]
        }
    });

你能告诉我我做错了什么吗?

php android mysql okhttp okhttp3
1个回答
1
投票

好,我知道了! Renan Arceno提醒我,我的Rest Service给了我HTML实体。所以不要在Java中使用Html.fromHtml(result) - 这显然也可以 - 我删除了PHP函数htmlentities(),它将我的字符转换为HTML实体。

现在它有效。谢谢你!

© www.soinside.com 2019 - 2024. All rights reserved.