【发布时间】: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 中并没有帮助。
编辑:现在可以了,我不知道为什么,我没有做任何更改。
【问题讨论】: