Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling accented characters

Tags:

json

android

php

I store place names from all over the world (for example Râşnov) which is stored in a database correctly, and when sent to/from php it is shown/saved correctly. And when sent to android it shown correctly, but when I try to send any accented characters (and some others) to the database via php from android, it fails, and stops reading the json at the first bad character.

Android:

final HttpClient client = new DefaultHttpClient(params);
final HttpPost poster = new HttpPost(Constants.COMMAND_URL);    
poster.addHeader("Content-Type", "application/json; charset=utf-8");
final String jsonString = cmd.getJSON().toString();    
poster.setEntity(new StringEntity(jsonString));    
final HttpResponse response = client.execute(poster);

php:

header('Content-Type: application/json; charset=utf-8', true,200);
$data = file_get_contents('php://input');
$json = json_decode($data, true); 

Example JSON:

{"Params":{"PageIndex":0,"PageSize":20,"SearchTerm":"râs"},"Request":"Search","SessionID":"3EF90227"}

When I get JSON from the server it shows as

{"Result":1,"Results":[{"PlaceName":"R\u00e2\u0219nov Citadel","Country":"Romania","OnlineID":"142","Location":"R\u00e2\u0219nov, Bra\u0219ov County","Category":"1"}]}

So it replaces â with \u00e2, can I do that from Android?

EDIT: To clarify, I need a way of sending accented characters to my database via php. If I send them as they are (ie. â) then the json_decode fails.

I have done

utf8_encode

in php and new String (oldstring.getBytes(),"UTF-8") in android and it didn't help.

EDIT: It works now, I don't know why, afaik I've made no changes.


1 Answers

This could be due to Android not accepting those specific characters and it replaces it with the associated code.

Only some versions of Android will accept all accented Characters.

Please have a look at the following link : http://forums.omgpop.com/forum-feedback/3519-android-app-cannot-add-accented-characters.html

https://android.stackexchange.com/questions/4229/is-it-possible-to-type-accented-letters-using-the-galaxy-s-english-keyboard

like image 86
DarkMantis Avatar answered Feb 23 '26 01:02

DarkMantis