1

In a nutshell, french characters are causing trouble when sending string from my android app to php and decoding it using JSON. Here is what I am doing in my android app (Java)

    HttpPost httppost = new HttpPost(//my server and filename);

    try {
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

       nameValuePairs.add(new BasicNameValuePair("payload", jsonObj.toString()));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       // Execute HTTP Post Request
       HttpResponse response = httpclient.execute(httppost);
     }....

And here is my php code

$_POST['payload'] = stripslashes($_POST['payload']); 
$payload = $_POST['payload'];
error_log(" $payload ", 0);
$payloadObj = json_decode($payload);
error_log(" $payloadObj ", 0);

When english letters are used then everything is perfect, however is perfect but when I accented frensh letters then it does not work. I inserted the above Error logs to see what I get and I noticed that with French letters, payload is showing the french letter as � and the payloadObj is empty so I guess the decode failed.

Pleeease help me out,where is exactly the problem happening (at what stage)? How can I solve it?

Snake
  • 14,228
  • 27
  • 117
  • 250
  • Why are you using `stripslashes()`? – Brad Nov 01 '12 at 03:31
  • Because I am using this info for mysql and it is the in case magicQuotes is on. It didnt do any harm with english letters – Snake Nov 01 '12 at 03:33
  • `stripslashes()` does **nothing** for MySQL, and has nothing at all to do with it. You should disable magic quotes. If you can't, check this answer out: http://stackoverflow.com/a/517027/362536 – Brad Nov 01 '12 at 03:34

1 Answers1

0

Had you try fix it using iconv() ?

//$payload = $_POST['payload'];
$payload = (isset($_POST['payload'])) ? iconv("UTF-8","UTF-8//IGNORE",$_POST['payload']) ? '';
error_log(" $payload ", 0);
$payloadObj = json_decode($payload);
error_log(" $payloadObj ", 0);
Danny Hong
  • 1,474
  • 13
  • 21
  • 1
    I will accept the answer just because it gave me hint about the correct solution. The correct solution is to do httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs), "UTF-8"); and that fixed it – Snake Nov 01 '12 at 19:25