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


How to get emoji from database MySQL using PHP in Android

我可以发送表情符号的unicode,但由于无法从PHP API获取unicode。

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

["'ud83dde00"]

现在在安卓系统中如何获得这个不知道并显示到表情包文本视图。我使用此示例在TextView和EditText中使用表情符号https://github.com/pepibumur/emojize

这是我的Java代码,我创建了一个API来发送unicode和相同的get作为响应,保存在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();

感谢

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

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

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