如何在Android中编码和解码表情符号? [英] how to encode and decode emoji in android?

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

问题描述

我在应用程序中使用了 https://github.com/rockerhieu/emojicon 库.当我在代码中传递静态unicode字符串时,表情符号可见,但是如果我使用常规的get webservice将表情符号发送到php服务器并检索该字符串,则它仅将unicode字符串显示到我的应用程序中.如果比较的话,静态字符串和服务器检索的字符串都是相同的.

I used https://github.com/rockerhieu/emojicon library in my application. When I pass a static unicode string in my code the emoji is visible but if I send the emoji to php server using regular get webservice and retrive the string then it just showing unicode string into my application. both static and server retrieved strings are same if I compare.

有人可以告诉我我对应用程序做了什么错误.在IOS中开发了相同的应用程序,他们所做的是在发送到服务器时首先将字符串编码为ASCII> UTF-8. 然后他们以与发送相同的方式对字符串进行解码.如果这也可以与android兼容,有人可以建议我吗,如果可以,我该怎么做.

Can anybody tell me what wrong I have done into my application. the same application is developed in IOS and what they did is they first encoding the string into ASCII>UTF-8 while sending to server. then they are decoding the string in same way as they send. Can anybody suggest me IF this would be compatible with android also, If yes then how can I do this.

推荐答案

我们可以使用commons-lang(commons-lang-2.5.jar)库对Unicode字符进行编码和解码.在此处下载jar文件或使用gradle:compile 'org.apache.commons:commons-lang3:3.4'.

We can use commons-lang(commons-lang-2.5.jar) library for encoding and decoding of the unicode characters. Download jar file here or use gradle: compile 'org.apache.commons:commons-lang3:3.4'.

用于编码-StringEscapeUtils.escapeJava(String text) 可以在调用getText方法的android EditText中使用它,在发送到Web服务器之前,它将正确编码unicode字符.

For Encoding use - StringEscapeUtils.escapeJava(String text) This can be used in android EditText when call getText method, where it will encode the unicode characters properly before sending to web server.

用于解码-StringEscapeUtils.unescapeJava(String text) 可以在android TextViewsetText中使用,在收到来自Web服务器的响应后,它将正确解码unicode字符.

For Decoding use - StringEscapeUtils.unescapeJava(String text) This can be used in android TextView to setText, where it will decode the unicode characters properly after receiving the response from web server.


例如:

EditText etEmojiEditText = new EditText(this);
etEmojiEditText.setText("TYPE SOMETHING IN EMOJI");

String toServer = etEmojiEditText.getText();
String toServerUnicodeEncoded = StringEscapeUtils.escapeJava(toServer);

String serverResponse = "SOME RESPONSE FROM SERVER WITH UNICODE CHARACTERS";
String fromServerUnicodeDecoded = StringEscapeUtils.unescapeJava(serverResponse);

仅供参考还将编码和解码用于Web服务端. Unicode编码的字符串应从Web服务中解码,并且Web服务的响应应在发送给客户端之前进行编码. 服务器表应包含utf8mb4而不是utf8,因为Unicode字符每个字符需要4个字节.因此unicode不会以3个字节表示.

FYI Use the encoding and decoding for web service side as well. Unicode encoded string should be decoded from web service and response from web service should be encoded before sending to clients. Server tables should contain utf8mb4 instead of utf8, because unicode character needs 4bytes per character. Therefore unicode will not be represented in 3bytes.

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

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