java - Send special characters in JSON from android to PHP -


i want send json php file have on server, works fine except when field contains special characters (accents, ñ, etc.).

java file:

    httpclient httpclient = new defaulthttpclient();     httppost httppost = new httppost(uri);      jsonobject json = new jsonobject();      try {         // json data:         json.put("id_u", viaje.getid_u());         json.put("id_vo", viaje.getid_vo());         json.put("titulo", viaje.gettitulo());         [...]          jsonarray postjson=new jsonarray();         postjson.put(json);          // post data:         httppost.setheader("json",json.tostring());         httppost.getparams().setparameter("jsonpost",postjson);          // execute http post request         system.out.print(json);         httpresponse response = httpclient.execute(httppost); 

php file:

$json = $_server['http_json']; $data = json_decode($json);  $id_u = $data->id_u; $id_vo = $data->id_vo; $titulo = $data->titulo; [...] 

for example, if titulo = "día", $title empty, instead whether titulo = "example" works correctly. not know how convert utf-8 before sending items, tried many things , nothing works me. idea?


edit:

i solve problem. clear problem encoding. solved adding 2 lines code:

java file:

// post data: httppost.setheader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); httppost.setheader("json",json.tostring()); httppost.getparams().setparameter("jsonpost",postjson); 

php file:

$json = $_server['http_json']; $cadena = utf8_encode($json); $data = json_decode($cadena); 

thanks help! :)

sounds encoding issue. try setting encoding this:

httppost httppost = new httppost(builder.geturl()); httppost.setheader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); 

you can force proper encoding on content this. that's not needed here:

// add data httppost.setentity(new urlencodedformentity(builder      .getnamevaluepairs(), "utf-8")); 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -