我正在开发一个能够以三种语言显示文字的应用程序。我通过PHP脚本从服务器获取数据。问题是阿拉伯语和库尔德语文本显示为?分数。我修改了代码以接受utf-8字符。数据显示为?即使在浏览器中打开也会标记。 mysql服务器的排序规则是utf8。此外,我已将静态和阿拉伯语文本静态添加到我的应用程序,它的工作完美。我也试过utf8_encode()而没有任何结果 -
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
// in = new BufferedReader(new InputStreamReader(response.getEntity()
// .getContent()));
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent(), "UTF-8"));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e("log_tag", "Error converting result " + e.toString());
e.printStackTrace();
}
}
}
}
我的代码中有什么问题吗?或者我还需要做其他事情吗?提前谢谢。
编辑:我使用以下php脚本编码json数据 -$subcat_id = mysql_real_escape_string(intval($_GET["id"]));
$query = mysql_query("select * from subcategory where category_id = $subcat_id")or die(mysql_error());
while($row = mysql_fetch_assoc($query))
$output[]=$row;
print(json_encode($output));
并且mysql服务器上使用的排序规则是utf8。再次感谢。
答案 0 :(得分:0)
使用mysql_query(“SET CHARACTER SET'utf8'”,$ connect);在建立数据库连接后 例如 -
$connect = mysql_connect(dbhost, user, password) or
die("MySQL Error: " . mysql_error());
$db = mysql_select_db(dbname) or die("MySQL Error: " . mysql_error());
mysql_query("SET CHARACTER SET 'utf8'", $connect)