如何在android中编码和解码表情符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25074112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to encode and decode emoji in android?
提问by Vishnu
I used https://github.com/rockerhieu/emojiconlibrary 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.
我在我的应用程序中使用了https://github.com/rockerhieu/emojicon库。当我在我的代码中传递一个静态 unicode 字符串时,表情符号是可见的,但是如果我使用常规 get webservice 将表情符号发送到 php 服务器并检索字符串,那么它只会在我的应用程序中显示 unicode 字符串。如果我比较,静态和服务器检索的字符串都是相同的。
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.
谁能告诉我我在我的应用程序中做错了什么。在 IOS 中开发了相同的应用程序,他们所做的是首先将字符串编码为 ASCII>UTF-8,同时发送到服务器。然后他们以与发送相同的方式解码字符串。任何人都可以建议我如果这也与android兼容,如果是,那么我该怎么做。
回答by Dulan Anuradha Bandara Dissana
We can use commons-lang(commons-lang-2.5.jar) library for encoding and decoding of the unicode characters. Download jar file hereor use gradle: compile 'org.apache.commons:commons-lang3:3.4'
.
我们可以使用 commons-lang(commons-lang-2.5.jar) 库对 unicode 字符进行编码和解码。在此处下载 jar 文件或使用 gradle: compile 'org.apache.commons:commons-lang3:3.4'
.
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.escapeJava(String text)
这可以EditText
在调用getText
方法时在 android 中使用,它将在发送到 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.
用于解码-StringEscapeUtils.unescapeJava(String text)
这可以在 android TextView
to 中使用setText
,它会在收到来自 Web 服务器的响应后正确解码 unicode 字符。
Ex:
前任:
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);
FYIUse 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.
仅供参考 也使用 Web 服务端的编码和解码。Unicode 编码的字符串应从 Web 服务解码,并且应在发送到客户端之前对来自 Web 服务的响应进行编码。服务器表应该包含 utf8mb4 而不是 utf8,因为 unicode 字符需要每个字符 4bytes。因此 unicode 不会以 3 字节表示。
回答by Neeraj Shukla
I have used the same library as you for emoji. I have used URLEncoder.encode(commentString, "UTF-8") for encoding and similarly, URLDecoder.decode(encodedComment, "UTF-8") for decoding.
我用和你一样的库来制作表情符号。我使用 URLEncoder.encode(commentString, "UTF-8") 进行编码,类似地,使用 URLDecoder.decode(encodedComment, "UTF-8") 进行解码。
Works perfectly for me. Hopefully for you too.
非常适合我。希望对你也有帮助。
回答by parambir singh
Emojis
needs to be encoded and decoded . You should send the text to server by encoding and show again in the app
by decoding the same. The code is below for the encryption and decryption.
Emojis
需要编码和解码。您应该通过编码将文本发送到服务器,并app
通过解码再次显示在服务器中。下面是加密和解密的代码。
public static String encodeEmoji (String message) {
try {
return URLEncoder.encode(message,
"UTF-8");
} catch (UnsupportedEncodingException e) {
return message;
}
}
public static String decodeEmoji (String message) {
String myString= null;
try {
return URLDecoder.decode(
message, "UTF-8");
} catch (UnsupportedEncodingException e) {
return message;
}
}
回答by Denshov
In my case all text in message disappears after symbols & and +, so I decided to replace them before sending and after receiving and it works well for me.
在我的情况下,消息中的所有文本在符号 & 和 + 之后都消失了,所以我决定在发送之前和接收之后替换它们,这对我来说效果很好。
private String encodeMessage(String message) {
message = message.replaceAll("&", ":and:");
message = message.replaceAll("\+", ":plus:");
return StringEscapeUtils.escapeJava(message);
}
private String decodeMessage(String message) {
message = message.replaceAll(":and:", "&");
message = message.replaceAll(":plus:", "+");
return StringEscapeUtils.unescapeJava(message);
}
回答by Gowtham Kumar
You can encode and decode emoticons without using any libraries in android if you are receiving unicode from your server .
如果您从服务器接收 unicode,则可以在不使用任何 android 库的情况下对表情符号进行编码和解码。
Example :
例子 :
U+1F601 is 16 bit unicode.
Replace U+
with 0x
.
替换U+
为0x
。
int originalUnicode=0x1F601;
textView.setText(getEmoticon(originalUnicode));
public String getEmoticon(int originalUnicode) {
return new String(Character.toChars(originalUnicode));
}
回答by Sabeer Mohammed
I am using the same library ... You don't need to encode or escape the emoji ... simply getText().toString() will work.
我正在使用相同的库......你不需要编码或转义表情符号......只需 getText().toString() 就可以了。
Follow the steps
按照步骤
In mysql change the field collation to utf8mb4_unicode_ci
(my database and table collation is utf8_unicode_ci)
在mysql中将字段排序规则更改为utf8mb4_unicode_ci
(我的数据库和表排序规则为utf8_unicode_ci)
Used PHP Codigniter on server so the database configuration
在服务器上使用 PHP Codiggniter,因此数据库配置
'char_set' => 'utf8mb4',
'dbcollat' => 'utf8mb4_unicode_ci'
Then in Android use textView.getText().toString();
then send the text without using any encode or escape
然后在 Android 中使用textView.getText().toString();
然后发送文本而不使用任何编码或转义
回答by Melih Toksari
Convert unicode to emoji in whole content without using any library.
在不使用任何库的情况下将整个内容中的 unicode 转换为表情符号。
private String convertEmoji(String content) {
content = content.replaceAll("U\+", "0x");
String keyword = "0x";
int index = content.indexOf(keyword);
int spaceIndex;
while (index >=0){
spaceIndex = content.indexOf(" ", index);
if(spaceIndex > index) {
String emoji = content.substring(index, spaceIndex);
content = content.replaceAll(emoji, getEmoticon(Integer.decode(emoji)));
}
index = content.indexOf(keyword, index+keyword.length());
}
return content;
}