[英]android and php json utf-8 fail
我在將utf-8從android傳輸到php時遇到問題。 我在網上查找了許多解決方案,但是所有這些解決方案都無法發送UTF-8。
我的Java代碼
String str = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
// set up post data
JSONObject json = getJSON();
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
httppost.setHeader("json",json.toString());
httppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
httppost.getParams().setParameter("jsonpost",postjson);
httppost.getParams().setParameter("jsonpost",postjson);
HttpResponse response = httpclient.execute(httppost);
if(response != null)
{
str = getInputString(response);
return str;
}
}
catch (UnsupportedEncodingException e) {
return "";
}
catch (Exception e) {
return "";
}
return "";
另一方面我的PHP
<?php
header'Content-Type:text/plain; charset=utf-8');
$json = $_SERVER['HTTP_JSON'];
$data = json_decode($json);
$message = $data->message;
echo $message;
?>
如果接收到的中文字符是“歡迎”,但如果輸入的是“ Welcome”,則我收到的輸出只是一個空白字符串。 該消息將具有歡迎輸出。 這里的主要問題是什么? 如果有人幫助,IT部門將非常感激。 謝謝。
解:
$data = json_decode(file_get_contents('php://input'));
$message = $data->message;
echo $message;
?>
為什么要使用BiteArray
? 您可以這樣簡單地做:
httpPost.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8)
其中,值( List<NameValuePair>
)將成為參數。
在您的ini上設置utf-8,也可以在php腳本上使用此功能:
ini_set('default_charset', 'utf-8');
將此代碼放在:
<?php
ini_set('default_charset', 'utf-8');
$json = $_SERVER['HTTP_JSON'];
$data = json_decode($json);
$message = $data->message;
echo $message;
?>
即使該問題的日期為2012年,似乎該問題仍未解決,並且同樣的問題仍未解決。
由於我正在使用平板電腦和PHP Web服務器之間使用法語和葡萄牙語語言在平板電腦和PHP Web服務器之間傳輸大量數據的APP進行工作,因此對於“ stange char”,我進行了一些測試:
使用Android Tablet將數據發送到PHP網站時,只需使用以下方式准備數據:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("op","_CID1")); nameValuePairs.add(new BasicNameValuePair("la",la)); nameValuePairs.add(new BasicNameValuePair("lo",lo)); nameValuePairs.add(new BasicNameValuePair("id",codigo)); // Encoding URL for GET String the_param = URLEncodedUtils.format(nameValuePairs,"utf-8"); String url = the_url + "?" + the_param;
在PHP方面,您必須使用isset測試值的存在(因此在本例中為isset($ _ GET ['op']);依此類推,然后必須使用SQL執行數據驗證,以便防止SQL注入和解碼de值,為此,只需使用:
$result = @mysqli_real_escape_string ($handle,$var);
其中hanlde是打開數據庫后獲得的句柄,而$ var是從Android獲得的值。 (示例使用SQLi)
一個簡單的方法是在PHP方面執行utf8_decode(我寫的DECODE不編碼!!)因此,如果您這樣做,則在PHP方面:
$str = "Não, l'été c'est pour bientôt";
echo $str;
在Android下,您會得到錯誤的字符串。 要獲得完美的價值,您必須執行以下操作:
$str = "Não, l'été c'est pour bientôt";
$str = utf8_decode($str);
echo $str;
要在Android上獲取字符串,只需執行以下操作:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url[0]);
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
text = EntityUtils.toString(ent);
說明:基本上,在PHP方面,字符串是UTF8。 執行utf8解碼時,請將字符串設置為ISO-8859-1(在Android上為defaut值)(請參閱EntityUtils文檔)。
如果您未在PHP端執行utf8_decode,則將使用utf8傳輸數據,而utaut在Android上是不知道的。 在這種情況下,您將必須使用參數utf8執行EntityUtils。
實際上,或者您在PHP方面將UTF8轉換為ISO-8859-1,然后傳輸並使用ISO-8859-1(選項1),或者您傳輸UTF8,因此需要在Android端使用EntityUtils(選項2)進行轉換。
希望這對彼得有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.