当我在 android 中创建 json 时,我将阿拉伯字符放入 json 中,就像下面的代码一样:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
JSONObject json = new JSONObject();
try {
json.put("jsonValue", "بسم الله ");
} catch(Exception e) {
}
JSONArray postjson = new JSONArray();
postjson.put(json);
httppost.setHeader("json", json.toString());
httppost.getParams().setParameter("jsonpost", postjson);
HttpResponse response = httpclient.execute(httppost);
php 代码包含charset_utf_8但结果不正确。PHP代码如下所示
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
$json = $_SERVER['HTTP_JSON'];
var_dump($data);
$data = json_decode($json);
$jsonValue= $data->jsonValue;
echo $jsonValue;
?>
结果打印成这样"(3E 'DDG",有人可以提供帮助吗?
你可以在
安卓中使用URLEncoder
,如下所示:
json.put("jsonValue",URLEncoder.encode(jsonValue, "utf-8"));
在你的PHP代码中使用urldecode:
$jsonValue= urldecode($data->jsonValue);
在
发送到服务器之前使用 URLEncoder
对字符串进行编码
URLEncoder.encode(str, "UTF-8");
您可以将编码格式从UTF更改为其他格式,例如JSON_UNESCAPED_UNICODE等
它
由以下方法修复:
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));