我正在尝试使用 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"}]}
最佳答案
我的数据库已经默认为utf8,遇到同样的问题。
添加了常用的meta标签“”后,问题依旧。
然后我创建了一个专用的 connection.php 以确保与 mysql 的所有通信都是字符集 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
?>
关于android - 在android中显示汉字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13303820/