【发布时间】: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"}]
}
});
你能告诉我我做错了什么吗?
【问题讨论】:
-
&ouml;是o umlaut的 html 表示法。 -
&auml;阅读a uml或a umlaut或ä -
是的,我知道
-
那为什么要说
Obviously there is something wrong? -
你可以通过这样做来解决这个问题:
Html.fromHtml(result)
标签: php android mysql okhttp okhttp3