在MySQL中存储Android表情符号 [英] Storing Android Emoji in MySQL

查看:112
本文介绍了在MySQL中存储Android表情符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将用户选择的表情符号存储在MySQL的文本主体中.我们的列看起来像这样

We are storing the emoji selected by the users in text body in MySQL. We have the column looks like this

`body` text CHARACTER SET utf8 COLLATE utf8_bin,

原始数据:<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">\uD83D\uDE1D\uD83D\uDE3E\uD83D\uDE3E\uD83D\uDE3A\uD83D\uDE3A\uD83D\uDE1D</div>

数据库中的数据:<div id='mobile-question-style' style="font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;">????????????</div>

但是,如果从Rest API获取它,它看起来是正确的:"body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">\uD83D\uDE1D\uD83D\uDE3E\uD83D\uDE3E\uD83D\uDE3A\uD83D\uDE3A\uD83D\uDE1D</div>",

But when if fetch it from the Rest API it looks correct : "body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">\uD83D\uDE1D\uD83D\uDE3E\uD83D\uDE3E\uD83D\uDE3A\uD83D\uDE3A\uD83D\uDE1D</div>",

现在,当我更新问题时,使用Rest API会松开其编码并显示"????"

Now when I update the question, using Rest API it looses it encoding and showes "????"

我更新后来自端点的数据:"body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">????????????</div>",

Data from endpoint after I update : "body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">????????????</div>",

更新车身时有特殊的逻辑,所以有人知道这里发生了什么吗?

There is special logic when updating the body so does any one knows what is going on here?

推荐答案

要将表情符号存储到Mysql数据库中,请将要存储的字符串转换为编码的Base 64字符串.在您的应用程序方面,您所要做的就是使用Base 64对该字符串进行解码.

To store emojis into a Mysql Database convert the string you are storing into an encoded Base 64 string. On your application side all you have to do is decode that string using Base 64.

编码

                byte[] data = editTextFieldWithEmojis.getText().toString().getBytes("UTF-8");
                base64String = Base64.encodeToString(data, Base64.DEFAULT);

解码

                byte[] data = Base64.decode(stringWithEmojis, Base64.DEFAULT);
                newStringWithEmojis = new String(data, "UTF-8");

这篇关于在MySQL中存储Android表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆