Android到php发送UTF8编码文本,不使用西里尔语或阿拉伯语字符
我需要将一个字符串从Android发送到一个php web服务(EditText中的用户输入)。 这运行正常,带有标准字符和一些特殊字符(如重音符号、ñ等),但当我发送西里尔语或阿拉伯语字符时,web服务无法识别,并替换为其他字符(如±ÐÒ)。 这是我的Android代码:Android到php发送UTF8编码文本,不使用西里尔语或阿拉伯语字符,php,android,json,utf-8,cyrillic,Php,Android,Json,Utf 8,Cyrillic,我需要将一个字符串从Android发送到一个php web服务(EditText中的用户输入)。 这运行正常,带有标准字符和一些特殊字符(如重音符号、ñ等),但当我发送西里尔语或阿拉伯语字符时,web服务无法识别,并替换为其他字符(如±ÐÒ)。 这是我的Android代码: // Post the data: public class Sincronizar extends AsyncTask<JSONObject, JSONObject, JSONObject> { Str
// 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);
}
}
//发布数据:
公共类Sincronizar扩展异步任务{
字符串resFromServer=null;
专用字符串url=”http://.../send.php“;//web服务的url
@凌驾
受保护的JSONObject doInBackground(JSONObject…数据){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
JSONObject jsonResponse=null;
试一试{
JSONObject jsonPlace=新的JSONObject();
jsonPlace.put(“pais”,((EditText)findViewById(R.id.etTexto)).getText().toString());
JSONArray postjson=新的JSONArray();
postjson.put(jsonPlace);
//发布数据:
setHeader(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=utf-8”);
setHeader(“json”,jsonPlace.toString());
httppost.getParams().setParameter(“jsonpost”,postjson);
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
if(response.getEntity()!=null){
resFromServer=org.apache.http.util.EntityUtils.toString(response.getEntity());
jsonResponse=新的JSONObject(resFromServer);
}
}捕获(例外e){
Log.e(“Sincronizar”,“doIn:+resFromServer”);
}
返回jsonResponse;
}
@凌驾
受保护的void onPostExecute(JSONObject jsonResponse){
试一试{
if(jsonResponse!=null){
stringpais=jsonResponse.getString(“pais”);
String respuesta=String.format(“PAI:%s”,PAI);
Log.e(“Sincronizar”,“respuesta”);
((TextView)findViewById(R.id.textView1)).setText(respuesta);
}
}捕获(例外e){
Log.e(“Sincronizar”,“onPostExecute:+e.getMessage());
}
onPostExecute(jsonResponse);
}
}
这是php web服务:
<?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)));
?>
在web服务中,我还尝试将收到的字符串插入Mysql数据库。正常字符和一些特殊字符,如¨,á(重音)等,都会被接收到。但我用阿拉伯语或西里尔字母收到这种字符串:“±‘’,“±‘‘‘‘”
我认为问题在于编码,但我发送和接收UTF8(我认为)
有什么想法吗?
谢谢 尝试此功能这可能会对您有所帮助
function parseUtf8ToIso88591($string){
if(!is_null($string)){
$iso88591_1 = utf8_decode($string);
$iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $string);
$string = mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');
return "";
}
return $string;
}
在php中运行服务器上的Sql操作之前
$mysqli->set_charset("utf8")
(或同等标准,我使用mysqli)
这解决了我的问题
mysqli_set_charset($con,'utf8');
mysqli_query($con, "insert into table (name, lastname) values ('Ñishá', 'Akopov')");
您可以在utf8中编写select语句并检索行
希望这会有帮助数据库中的排序规则是什么?