【问题标题】:How to get emoji from database MySQL using PHP in Android如何在 Android 中使用 PHP 从数据库 MySQL 中获取表情符号
【发布时间】:2015-06-13 09:36:45
【问题描述】:

我能够发送 Emoji 的 unicode,但一直坚持从 PHP API 获取 unicode。

这里我得到了 JsonArray 对象的响应,因为现在只是测试目的,我只是 JsonArray,因为我将来也需要。

["\ud83dde00"]

现在在 Android 中如何获取这个不知道并显示到 EmojiTextView。我用这个例子在TextView和EditText中使用Emoji https://github.com/pepibumur/emojize

这是我的 Java 代码,我创建了一个 API 来发送 unicode 并得到相同的响应,保存在数据库中,工作完美,只是在这里卡住了一点。

new AsyncTask<Void, Void, Object>() {
        ProgressDialog pDialog;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = ProgressDialog.show(MainActivity.this, "", "please wait");
        }
        @Override
        protected Object doInBackground(Void... params) {
            try {
                String contents;

                HttpPost request = new HttpPost("http://172.16.16.44/test/save_emoji.php");
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("addEmoji", s+""));
                request.setEntity(new UrlEncodedFormEntity(param,"UTF-8"));

                DefaultHttpClient client = new DefaultHttpClient();
                HttpResponse response = client.execute(request);
                response.setHeader("Content-Type", "application/json; charset=utf-8");
                InputStream is = response.getEntity().getContent();
                InputStreamReader(is));

                byte[] buffer = new byte[1024];
                int nread;
                while ((nread = is.read(buffer)) > 0) {
                    baos.write(buffer, 0, nread);             
                }
                is.close();
                response = null;
                is = null;
                client = null;
                return parseJson();

            }catch (JSONException e) {
                e.printStackTrace();
                try {
                    return new String(baos.toByteArray(), "UTF-16");
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                    return e1.getMessage();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return e.getMessage();
            }
        }

        @SuppressLint("NewApi")
        private String parseJson() throws JSONException, UnsupportedEncodingException {
            String jsonText = new String(baos.toByteArray());
            Log.e("emoji", "resp "+jsonText);
            JSONArray jarr = new JSONArray(jsonText);
            for(int i=0;i<jarr.length();i++){
                Log.e("emoji", new String(jarr.get(i).toString().getBytes(),"UTF-8"));
                return jarr.getString(i);
            }
            return "";
        }

        protected void onPostExecute(final Object result) {
            super.onPostExecute(result);
            if(pDialog!=null)
                pDialog.dismiss();

            MainActivity.this.runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    try {
                        Log.e("emoji", "ss "+result);
                        mTxtEmojicon.setText(new String(result.toString().getBytes()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            });


        }

    }.execute();

谢谢

【问题讨论】:

    标签: php android mysql unicode emoji


    【解决方案1】:

    我得到了解决方案,它只是通过更多的编写代码而不是使用 Json 编码。

    PHP 代码

    $dbLink = mysql_connect(HOSTNAME, USERNAME, PASSWORD)or die(mysql_error());
    mysql_select_db(DATABASE, $dbLink)or die(mysql_error());
    
    mysql_query("SET character_set_client=utf8mb4", $dbLink);
    mysql_query("SET character_set_connection=utf8mb4", $dbLink);
    mysql_query("SET character_set_results=utf8mb4", $dbLink);
    
    $sql_query = "SELECT * FROM emoji";
    $dbResult = mysql_query( $sql_query, $dbLink)or die(mysql_error());
    $jason = "[";
    $addcoma  = "";
    while($rw=mysql_fetch_array($dbResult))
    {
        $jason .= $addcoma . ' {"message" : "'.$rw["message"].'"}';
        $addcoma = ",";
    }
    
    $jason .= "]";
    echo $jason;
    

    通过硬编码准备 Json 对象,得到你想要的结果。

    【讨论】:

    • 你拯救了我的一天。谢谢。
    【解决方案2】:

    选择数据,然后对字符串进行 json_decode,您将获得表情符号。

        json_decode($text) // $text contains the text with emoji
    

    【讨论】:

      【解决方案3】:

      用途:

      mysqli_set_charset($connection_variable,"utf8mb4"); 
      

      查询执行前。

      【讨论】:

        猜你喜欢
        • 2017-03-01
        • 2015-09-18
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 2016-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多