Android to php send UTF8 encoded text without working with cyrillic or arabic characters
I need to post a string from Android to a php web service (user input to EditText). This works fine, with standard characteristics and some special characters (like accents, etc.), but when I send Cyrillic or Arabic characters, the web service does not recognize then and replaces other characters (eg  ± Ð). This is my android code:
// Post the data:
public class Sincronizar extends AsyncTask<JSONObject, JSONObject, JSONObject> {
String resFromServer=null;
private String url = "http://.../send.php"; //The url of the web service
@Override
protected JSONObject doInBackground(JSONObject... data) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
JSONObject jsonResponse = null;
try {
JSONObject jsonPlace = new JSONObject();
jsonPlace.put("pais", ((EditText)findViewById(R.id.etTexto)).getText().toString());
JSONArray postjson=new JSONArray();
postjson.put(jsonPlace);
// Post the data:
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
httppost.setHeader("json",jsonPlace.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
if (response.getEntity()!=null) {
resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
jsonResponse=new JSONObject(resFromServer);
}
} catch (Exception e) {
Log.e("Sincronizar","doIn:"+resFromServer);
}
return jsonResponse;
}
@Override
protected void onPostExecute(JSONObject jsonResponse) {
try{
if (jsonResponse!=null) {
String pais = jsonResponse.getString("pais");
String respuesta = String.format("PAIS: %s",pais);
Log.e("Sincronizar",""+respuesta);
((TextView)findViewById(R.id.textView1)).setText(respuesta);
}
}catch (Exception e) {
Log.e("Sincronizar","onPostExecute: "+e.getMessage());
}
super.onPostExecute(jsonResponse);
}
}
And this is a php webservice:
<?php
header('Content-Type: text/html; charset=utf-8');
try {
if (isset($_SERVER['HTTP_JSON'])) {
$json = $_SERVER['HTTP_JSON'];
$cadena = utf8_encode($json);
$data = json_decode($cadena,true);
$json_error= json_last_error();
$pais = $data["pais"];
} else {
$pais="No received";
}
}catch(Exception $e) {
$pais="Exception: ".$e->getMessage();
}
$output=array('pais'=>$pais);
print(utf8_encode(json_encode($output)));
?>
In the web service, I am also trying to insert the string I received into the Mysql database. Normal and some special characters such as Ñ, á (accents), etc., are accepted by e inserted ok. But with Arabic or Cyrillic characters I get strings like "± a", "± Ã'À"
I think the problem is with the encoding, but I am sending and receiving UTF8 (I think) Any ideas? thank!
source to share
Before executing Sql opeatrion on server in php run
$mysqli->set_charset("utf8")
(or equivalent, i used mysqli) This solved my problem
mysqli_set_charset($con,'utf8');
mysqli_query($con, "insert into table (name, lastname) values ('Ñishá', 'Akopov')");
You can write select statement and get strings in utf8
hope this helps
source to share