当我在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”,任何人都可以帮助PLZ吗?
答案 0 :(得分:2)
您可以在android中使用 URLEncoder
,如下所示:
json.put("jsonValue",URLEncoder.encode(jsonValue, "utf-8"));
并在您的PHP代码中使用urldecode:
$jsonValue= urldecode($data->jsonValue);
答案 1 :(得分:0)
在发送到服务器之前使用URLEncoder
对字符串进行编码
URLEncoder.encode(str, "UTF-8");
您可以将编码格式从UTF更改为其他格式,例如JSON_UNESCAPED_UNICODE等
答案 2 :(得分:-1)
由此确定:
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));