如何在Android中使用PHP从数据库MySQL获取表情符号

时间:2015-04-08 12:21:01

标签: php android mysql unicode emoji

我能够发送表情符号的unicode,但仍然坚持从PHP API获取unicode。

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

["\ud83dde00"]

现在在Android中如何让它不知道并显示给EmojiTextView。我使用此示例在TextView和EditText https://github.com/pepibumur/emojize

中使用表情符号

这是我的Java代码,我创建了一个API来发送unicode和同样得到的响应,保存在DB工作完美只是卡在这里。

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

由于

3 个答案:

答案 0 :(得分:2)

我得到了解决方案,它只是通过更多编写代码而不是使用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对象,得到你想要的结果。

答案 1 :(得分:0)

选择数据,只需json_decode字符串即可获得表情符号。

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

答案 2 :(得分:-1)

使用:

mysqli_set_charset($connection_variable,"utf8mb4"); 

在查询执行之前。