36

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.

Vishnu
  • 711
  • 2
  • 6
  • 11
  • http://stackoverflow.com/questions/14488503/ios-cannot-decode-emoji-unicode-in-json-format-correctly-and-emoji-icons-are-di – Avinash Babu Aug 01 '14 at 06:26
  • @AvinashBabu:- need in android and have any idea how to encode and decode bcz have to send on server and get so please give me some advise......Thanks – Vishnu Aug 01 '14 at 07:28
  • Hey @Vishnu did you find how to send emoji to the server?Thanks in Advance. – Robin Royal Sep 30 '14 at 05:53

8 Answers8

73

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.

AljoSt
  • 439
  • 4
  • 19
  • I am also using the same library to handle emoticons.I am encoding the emoji to unicode characters using String toServerUnicodeEncoded = StringEscapeUtils.escapeJava(toServer) and decoding using String fromServerUnicodeDecoded = StringEscapeUtils.unescapeJava(serverResponse) . While displaying the decoded emoji in EmojiconTextView ,unicode is displayed .Its corresponding Emoji is not displayed .Please help me to fix the issue . – Deepak Rattan Feb 17 '16 at 07:31
  • I Got IllegalArgumentException with this library : **Less than 4 hex digits in unicode value: '\u062' due to end of CharSequence** – Iman Marashi Mar 31 '17 at 15:27
  • 4
    The updated version of compile 'org.apache.commons:commons-lang3:3.4' i.e, 3.7 has deprecated **StringEscapeUtils** . Use implementation 'org.apache.commons:commons-text:1.3' instead of compile 'org.apache.commons:commons-lang3:3.4' . – Nainal Apr 13 '18 at 12:03
  • And make sure to import StringEscapeUtils from org.apache.commons:commons-text:1.3 – Nainal Apr 13 '18 at 12:32
  • 1
    AWESOME . after 3 hours of research :D – Jean Raymond Daher Feb 28 '21 at 02:57
9

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.

Neeraj Shukla
  • 1,271
  • 11
  • 13
  • this is working fine but this does not provide correct unicode – Anand Kumar Jha May 25 '16 at 12:33
  • Please explain "does not provide correct unicode", are you not getting it in "UTF-8" format? and how have you determined that you are not getting the encoding in utf-8 ? – Neeraj Shukla May 26 '16 at 10:43
  • I have also written that it is working fine just for encoding but in case to get native emoji text that can be used by other application is not possible by this. – Anand Kumar Jha May 26 '16 at 12:18
  • Here first you encode the text including the emoji. Any other place if you decode the content, you would get the same emoji as long as that other place also supports utf-8. – Neeraj Shukla May 26 '16 at 12:34
  • Can u please help me to convert the "5794d5f7895fa10a8f8e1357" into the emoji @NeerajShukla Thanks – Ravindra Kushwaha Jul 25 '17 at 10:52
6

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;
}
} 
parambir singh
  • 224
  • 2
  • 13
2

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);
}
DeniSHow
  • 1,394
  • 1
  • 18
  • 30
1

I am using the same library ... You don't need to encode or escape the emoji ... simply getText().toString() will work.

Follow the steps

In mysql change the field collation to utf8mb4_unicode_ci (my database and table collation is utf8_unicode_ci)

Used PHP Codigniter on server so the database configuration

'char_set' => 'utf8mb4',
'dbcollat' => 'utf8mb4_unicode_ci'

Then in Android use textView.getText().toString(); then send the text without using any encode or escape

Sabeer
  • 3,940
  • 1
  • 24
  • 20
1

You can encode and decode emoticons without using any libraries in android if you are receiving unicode from your server .

Example :

U+1F601 is 16 bit unicode.

Replace U+ with 0x.

  int originalUnicode=0x1F601;
  textView.setText(getEmoticon(originalUnicode));


  public String getEmoticon(int originalUnicode) {
    return new String(Character.toChars(originalUnicode));
}
Gowtham Kumar
  • 534
  • 8
  • 22
1

Convert unicode to emoji in whole content without using any library.

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;
}
0
  // convert UTF-8 to internal Java String format
    public static String convertUTF8ToString(String s) {
        String out = null;
        out = new String(s.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        return out;
    }

    // convert internal Java String format to UTF-8
    public static String convertStringToUTF8(String s) {
       return new String(s.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
    }
Sujith S Manjavana
  • 1,417
  • 6
  • 33
  • 60