我想首先告诉我,我不是一个Android开发者,所以请原谅我,如果我错过了一些明显的东西。
我正在使用Android Studio创建这个相对简单的应用程序,允许用户显示其数据库中的项目。每个表都具有相同的列名“ITEMID,PICURLS,DSCR,PRICE”。所有表都按原样返回它们的值,但是DSCR总是返回Questionmarks而不是我放在那里的简体中文字符。我已经尝试将排序规则设置为不同的设置(utf8_unicode_co,GB2312_bin,GB2312_chinese_ci,GBK_bin和GBK_chinese_ci),因为这是我每次用Google搜索时发现的。不幸的是,仍然没有成功......
检索这些表是由一个简单的PHP脚本完成的,我的Android应用程序正在使用HttpsURLConnection和BufferedReader调用它们。这是应用程序中唯一一段代码,用于发送和接收数据库。
if (command.equals("GETNEWESTITEMS")) {
try {
URL url = new URL("hidden_for_privacy_reasons");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
StringBuilder builder = new StringBuilder();
builder.append(URLEncoder.encode("command", "UTF-8"));
builder.append("=");
builder.append(URLEncoder.encode(command, "UTF-8"));
builder.append("&");
builder.append(URLEncoder.encode("category", "UTF-8"));
builder.append("=");
builder.append(URLEncoder.encode(category, "UTF-8"));
String urlParameters = builder.toString();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder responseOutput = new StringBuilder();
while ((line = br.readLine()) != null) {
Log.e("DatabaseTask", line);
responseOutput.append(line);
}
br.close();
echoData = responseOutput.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这将是我的PHP脚本执行此特定命令的唯一部分(可能不是最好的方法,但除简体中文字符外一切正常)
if ($command == "GETNEWESTITEMS") {
$category = $_POST["category"];
$qry = "select * from ".$category." ORDER BY id DESC LIMIT 5";
if ($result = mysqli_query($conn, $qry)) {
echo "succes&";
while ($row = mysqli_fetch_assoc($result)) {
echo implode(',', $row).":";
}
} else {
echo "failed&".mysqli_error($conn);
}
我唯一能想到的是它与我的应用程序读取从我的帖子请求返回的响应的方式有关,因为我打印的整行返回到日志,但我没有如果这是真的想法,以及如何解决它。
欢迎所有建议!
EDIT_01
if ($command == "GETNEWESTITEMS") {
$category = $_POST["category"];
$qry = ("set character_set_server='utf8'");
$qry = "select * from ".$category." ORDER BY id DESC LIMIT 5";
if ($result = mysqli_query($conn, $qry)) {
echo "succes&";
while ($row = mysqli_fetch_assoc($result)) {
echo implode(',', $row).":";
}
} else {
echo "failed&".mysqli_error($conn);
}
答案 0 :(得分:0)
使用
$qry = ("set character_set_server='utf8'");
在您的以下声明之前
$qry = "select * from ".$category." ORDER BY id DESC LIMIT 5";