如何在android中编码和解码表情符号?

时间:2022-01-12 00:15:12

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.

我在我的应用程序中使用了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,如果是的话,我怎么能这样做。

6 个解决方案

#1


52  

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'.

我们可以使用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)这可以在调用getText方法时在android EditText中使用,它将在发送到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中用于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);

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.

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

#2


11  

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.

对我来说很完美。希望也适合你。

#3


1  

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 Codigniter进行数据库配置

'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();然后发送文本而不使用任何编码或转义

#4


1  

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));
}

#5


0  

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);
}

#6


0  

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.

需要对表情符号进行编码和解码。您应该通过编码将文本发送到服务器,然后通过对其进行解码再次在应用程序中显示。下面的代码用于加密和解密。

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;
}
} 

#1


52  

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'.

我们可以使用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)这可以在调用getText方法时在android EditText中使用,它将在发送到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中用于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);

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.

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

#2


11  

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.

对我来说很完美。希望也适合你。

#3


1  

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 Codigniter进行数据库配置

'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();然后发送文本而不使用任何编码或转义

#4


1  

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));
}

#5


0  

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);
}

#6


0  

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.

需要对表情符号进行编码和解码。您应该通过编码将文本发送到服务器,然后通过对其进行解码再次在应用程序中显示。下面的代码用于加密和解密。

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;
}
}