duanjiu1003
duanjiu1003
2016-10-08 02:50

从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 doujiayao8433 5年前

    Use

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

    before your below statement

    $qry = "select * from ".$category." ORDER BY id DESC LIMIT 5"; 
    
    点赞 评论 复制链接分享

相关推荐