When I create json in android then I put arabic character in json like code below:
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);
The php code contains charset_utf_8 but the result is not correct. php code is shown below
<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;
?>
The result printed like this "(3E 'DDG", could any one show help plz?
You can use URLEncoder
in android like below:
json.put("jsonValue",URLEncoder.encode(jsonValue, "utf-8"));
and in your php code use urldecode:
$jsonValue= urldecode($data->jsonValue);
Encode string using URLEncoder
before sending to server
URLEncoder.encode(str, "UTF-8");
You can change encoding format from UTF to other format e.g. JSON_UNESCAPED_UNICODE etc
It Is Fixed by This :
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));