1

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?

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Ra'fat Haj Ali
  • 111
  • 1
  • 1
  • 11

3 Answers3

2

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);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
SYahya
  • 46
  • 1
  • 4
0

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

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
-1

It Is Fixed by This :

httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
smartrahat
  • 5,381
  • 6
  • 47
  • 68