Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to encode and decode emoji in android?

Tags:

android

emoji

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.

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.

like image 616
Vishnu Avatar asked Aug 01 '14 06:08

Vishnu


2 Answers

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

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.

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.


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.

like image 175
Dulan Anuradha Bandara Dissana Avatar answered Sep 21 '22 06:09

Dulan Anuradha Bandara Dissana


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.

Works perfectly for me. Hopefully for you too.

like image 24
Neeraj Shukla Avatar answered Sep 20 '22 06:09

Neeraj Shukla