简体   繁体   中英

Storing Android Emoji in 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,

Original data : <div id='mobile-question-style' style=\\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\\">\?\?\?\?\?\?\?\?\?\?\?\?</div>

Data in DB : <div id='mobile-question-style' style="font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;">????????????</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;\\">\?\?\?\?\?\?\?\?\?\?\?\?</div>", "body":"<div id='mobile-question-style' style=\\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\\">\?\?\?\?\?\?\?\?\?\?\?\?</div>",

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

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

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.

Encoding

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

Decoding

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM