[英]display chinese characters in android
我試圖使用PHP傳遞數據(來自服務器),數據由中文字符組成。 我試圖使用我設法檢索數據的JSON解析器解析數據,但數據(中文)顯示為???
。 誰能告訴我出了什么問題?
下面是我的解析器:
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
正如我的logcat所見。
11-09 07:18:53.939: D/Single Product Details(570): {"success":1,"search":[{"tel1":"67546498","generalEmail":"anderlab01@yahoo.com.sg","contactEmail":"jeff1285@hotmail.com","lineOfBusiness":"??????????? Manufacture,Import\/Export","postCode1":"738733","companyNameEng":" ANDER LAB PTE LTD ","repName1Eng":"Li Jianfu","address1":"Blk 20 Woodlands Link #04-40","fax1":"67544092","repName1Chi":"???","url":"www.bosun.com.sg"}]}
我的db已經默認為utf8,遇到同樣的問題。
添加通常的元標記“”后,問題仍然存在。
然后我創建了一個專用的connection.php來確保與mysql的所有通信都是charset utf8,注意mysqli_set_charset($ bd,'utf8')中的utf8中沒有“ - ” !
Connection.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_database = "dbname";
$prefix = "";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysqli_select_db($bd, $mysql_database) or die("Could not select database");
if(!mysqli_set_charset($bd, 'utf8')) {
exit() ;
}
?>
這是發送檢索數據的示例。
<?php
//Include database connection details
require_once('connection.php');
enter code here
//Create query
$qry = "SELECT * FROM subject";
$result = mysqli_query($bd, $qry);
// other stuff
?>
解決了。 您需要在utf8中對POST進行編碼。
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8")); <------ utf8
public static String test(){
String testResult= "";
try {
HttpGet get = new HttpGet("http://xxxxx");//edit url removed.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(get);
String result = EntityUtils.toString(response.getEntity());
JSONObject obj = new JSONObject(result);
if(!obj.isNull("title")){
testResult= obj.getString("title");
Log.d("Test","Test1:"+ testResult);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return testResult;//
}
從這段代碼中獲取幫助..這肯定會對你有所幫助..享受
您不提供檢索數據的代碼段。 這是我猜測源字符集是UTF-8,相應地替換你的源編碼。
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8"))), Charset.forName("UTF-8"));
str是你的json字符串。
更新:似乎您使用iso-8859-1
檢索,因此嘗試將您的代碼更新為UTF-8
。
注意:JSON文本的字符編碼始終為Unicode。 UTF-8是唯一在線上有意義的編碼,但也允許使用UTF-16和UTF-32( 源 )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.