【问题标题】:Handling accented characters处理重音字符
【发布时间】:2012-01-04 14:17:44
【问题描述】:

我存储了来自世界各地的地名(例如 Râşnov),这些地名正确存储在数据库中,并且当发送到/从 php 发送时,它会正确显示/保存。当发送到 android 时,它显示正确,但是当我尝试通过 php 从 android 将任何重音字符(和其他一些字符)发送到数据库时,它会失败,并在第一个坏字符处停止读取 json。

安卓:

final HttpClient client = new DefaultHttpClient(params);
final HttpPost poster = new HttpPost(Constants.COMMAND_URL);    
poster.addHeader("Content-Type", "application/json; charset=utf-8");
final String jsonString = cmd.getJSON().toString();    
poster.setEntity(new StringEntity(jsonString));    
final HttpResponse response = client.execute(poster);

php:

header('Content-Type: application/json; charset=utf-8', true,200);
$data = file_get_contents('php://input');
$json = json_decode($data, true); 

示例 JSON:

{"Params":{"PageIndex":0,"PageSize":20,"SearchTerm":"râs"},"Request":"Search","SessionID":"3EF90227"}

当我从服务器获取 JSON 时,它显示为

{"Result":1,"Results":[{"PlaceName":"R\u00e2\u0219nov Citadel","Country":"Romania","OnlineID":"142","Location":"R\u00e2\u0219nov, Bra\u0219ov County","Category":"1"}]}

所以它用 \u00e2 替换了 â,我可以在 Android 上这样做吗?

编辑:澄清一下,我需要一种通过 php 将重音字符发送到我的数据库的方法。如果我按原样发送它们(即 â),则 json_decode 失败。

我已经完成了

utf8_encode

在 php 和 new String (oldstring.getBytes(),"UTF-8") 在 android 中并没有帮助。

编辑:现在可以了,我不知道为什么,我没有做任何更改。

【问题讨论】:

    标签: php android json


    【解决方案1】:

    这可能是由于 Android 不接受这些特定字符并将其替换为相关代码。

    只有部分 Android 版本会接受所有重音字符。

    请查看以下链接: http://forums.omgpop.com/forum-feedback/3519-android-app-cannot-add-accented-characters.html

    https://android.stackexchange.com/questions/4229/is-it-possible-to-type-accented-letters-using-the-galaxy-s-english-keyboard

    【讨论】:

    • 服务器发送\u00e2 android 将它从那个转换为â(使用HTML.fromHTML),当我输入â 时android 发送â。这就是问题所在,我需要发送\u00e2。我尝试使用 HTML.toHTML,但它没有改变重音字符,只是在文本中添加标签。
    • @raybritton:你为什么需要这样做?如果它是 UTF-8 编码的,那不是正确的 json 吗? (只是要求将其固定,也许您可​​以通过告诉 android json 组件使用哪种编码)
    • 不确定这是否有帮助,但您可以通过此链接调试您发送/接收的内容:fileformat.info/info/unicode/char/e2/index.htm FYI,我花了很长时间才找到:P
    • @raybritton 我同意,如果它是 UTF-8 编码的,它可能会起作用。 php.net/utf8_encode 仅供参考
    • @hakre,因为 phps json_decode 由于某种原因无法读取重音字符。
    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2011-03-08
    • 2021-01-28
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    相关资源
    最近更新 更多