アプリケーションで https://github.com/rockerhieu/emojicon libraryを使用しました。コードに静的なユニコード文字列を渡すと、絵文字が表示されますが、通常のget webserviceを使用して絵文字をphpサーバーに送信し、文字列を取得すると、ユニコード文字列がアプリケーションに表示されます。比較すると、静的文字列とサーバー検索文字列は同じです。
誰が私がアプリケーションに間違ったことを教えてくれますか?同じアプリケーションがIOSで開発されており、サーバーへの送信中に最初に文字列をASCII> UTF-8にエンコードします。その後、送信と同じ方法で文字列をデコードします。これがAndroidと互換性がある場合、誰でも私に提案できますか?.
Unicode文字のエンコードとデコードにはcommons-lang(commons-lang-2.5.jar)ライブラリを使用できます。 jarファイルをダウンロード こちら またはgradle:_compile 'org.Apache.commons:commons-lang3:3.4'
_を使用します。
エンコーディングで使用する場合-StringEscapeUtils.escapeJava(String text)
これはAndroid EditText
getText
メソッドを呼び出すと、Webサーバーに送信する前にUnicode文字が適切にエンコードされます。
デコード用-StringEscapeUtils.unescapeJava(String text)
これはAndroid TextView
setText
へ。Webサーバーから応答を受信した後、Unicode文字を適切にデコードします。
例:
_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 [〜#〜]Webサービス側にもエンコードとデコードを使用します。 Unicodeエンコードされた文字列はWebサービスからデコードされ、Webサービスからの応答はクライアントに送信する前にエンコードされる必要があります。 サーバーテーブルには、utf8ではなくutf8mb4を含める必要があります。これは、Unicode文字には文字ごとに4バイトが必要だからです。したがって、Unicodeは3バイトで表されません。
私はあなたと同じライブラリを絵文字に使用しました。エンコードにはURLEncoder.encode(commentString、 "UTF-8")を使用し、デコードには同様にURLDecoder.decode(encodedComment、 "UTF-8")を使用しました。
私にぴったりです。うまくいけばあなたも。
AndroidサーバーからUnicodeを受信している場合は、ライブラリを使用せずに絵文字をエンコードおよびデコードできます。
例:
U+1F601 is 16 bit unicode.
置換U+
with 0x
。
int originalUnicode=0x1F601;
textView.setText(getEmoticon(originalUnicode));
public String getEmoticon(int originalUnicode) {
return new String(Character.toChars(originalUnicode));
}
私は同じライブラリを使用しています...絵文字をエンコードまたはエスケープする必要はありません...単にgetText()。toString()が機能します。
手順に従う
Mysqlでフィールド照合を_utf8mb4_unicode_ci
_に変更します(データベースとテーブルの照合はutf8_unicode_ciです)
PHPサーバー上のCodigniterを使用するため、データベース構成
_'char_set' => 'utf8mb4',
'dbcollat' => 'utf8mb4_unicode_ci'
_
その後、Android use textView.getText().toString();
で、エンコードまたはエスケープを使用せずにテキストを送信します
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;
}
}
私の場合、メッセージ内のすべてのテキストは記号&および+の後に消えるので、送信前と受信後にそれらを置き換えることにしました。
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);
}
ライブラリを使用せずに、コンテンツ全体で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;
}