【问题标题】:Calling PHP REST API from Android App doesn't display umlauts (äüö) correctly从 Android 应用程序调用 PHP REST API 无法正确显示变音符号 (äüö)
【发布时间】:2018-06-18 17:47:56
【问题描述】:

我在 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öö","content":"bär"}]

显然字符集或类似的东西有问题。

这是我的 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öö","content":"bär"}]
        }
    });

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

【问题讨论】:

  • öo umlaut 的 html 表示法。
  • ä 阅读 a umla umlautä
  • 是的,我知道
  • 那为什么要说Obviously there is something wrong
  • 你可以通过这样做来解决这个问题:Html.fromHtml(result)

标签: php android mysql okhttp okhttp3


【解决方案1】:

好的,我明白了! Renan Arceno 提醒我,我的 Rest Service 正在给我 HTML 实体。因此,我没有在 Java 中使用 Html.fromHtml(result)(显然也可以),而是删除了 PHP 函数 htmlentities(),它将我的字符转换为 HTML 实体。

现在可以了。谢谢你!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多