duanjiu1003 2016-10-08 02:50
浏览 62

从MySQL数据库中检索简体中文到Android应用程序[重复]

This question already has an answer here:

I want to start by telling I'm not an android developer by trade, so pardon me if I am missing something obvious.

I am using Android Studio to create this relative simple App which allows the user to show items from his Database. Each table has the same column names "ITEMID, PICURLS, DSCR, PRICE". All tables return their values as they should, however DSCR always returns Questionmarks instead of the Simplified Chinese characters I put in there. I have tried setting the collation to different settings (utf8_unicode_co, GB2312_bin, GB2312_chinese_ci, GBK_bin and GBK_chinese_ci) as this is what I found everytime I Googled for it. Unfortunately, still no success...

The retrieving of those tables are done by a simple PHP script, and is being called for by my Android Application using a HttpsURLConnection and BufferedReader. This is the only piece of code in the application that does the sending and receiving to my database.

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();
            }
        }

This would be the only part of my PHP script that does this particular command (probably not the best way, but everything works except for the simplified chinese characters)

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);
        }

The only thing I can think of is that it has something to do with the way my application reads the response that comes back from my post request since I print the entire line that gets is returned to the log, but I have no idea if this is true, and how to solve it.

All suggestions are welcome!

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);
        }
</div>
  • 写回答

1条回答 默认 最新

  • doujiayao8433 2016-10-08 08:32
    关注

    Use

    $qry = ("set character_set_server='utf8'");
    

    before your below statement

    $qry = "select * from ".$category." ORDER BY id DESC LIMIT 5"; 
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?