java和python中的Unicode字符串支持
我有一个Android应用程序,我在其中阅读短信并将其发送到谷歌应用程序引擎服务器。一些用户抱怨某些语言不能正常使用java和python中的Unicode字符串支持,java,android,python,google-app-engine,unicode,Java,Android,Python,Google App Engine,Unicode,我有一个Android应用程序,我在其中阅读短信并将其发送到谷歌应用程序引擎服务器。一些用户抱怨某些语言不能正常使用 // Execute query cursor = context.getContentResolver().query( SMS_PROVIDER_URI, SMS_QUERY_FIELDS, "date >= " + startDate.get
// Execute query
cursor = context.getContentResolver().query(
SMS_PROVIDER_URI,
SMS_QUERY_FIELDS,
"date >= " + startDate.getTime(), // selection - get messages > startDate
null, // selectionArgs
"date ASC"); // order - get oldest messages first
// Iterate results
if (cursor != null && cursor.moveToFirst()) {
// read through all the sms and create a list
do {
String sender = cursor.getString(0);
String message = cursor.getString(2);
boolean isIncomingMessage = cursor.getString(3).contains("1");
Date date = new Date(cursor.getLong(1));
String contactName = ContactLookup.lookup(context, sender);
smsList.add(new SMSMessageInfo(sender, contactName,
message, isIncomingMessage, date));
} while (cursor.moveToNext());
}
message变量包含来自不同语言的sms消息。我如何支持它?
此外,我需要将其发送到我的服务器(python)以及如何在服务器上翻译unicode?在python 2.7中,有两类字符串,
str
(标准字符串,由字节组成)和unicode
(由unicode字符组成,使用u前缀表示为文字:u“foo”)。转换通过使用实例上的方法完成:
u"blä".encode('utf8') → "bl\xc3\xa4" # from unicode to str
"bl\xc3\xa4".decode('utf8') → u"blä" # from str to unicode
转换通常是隐式进行的,例如。G如果将str
添加到unicode
,则在连接之前,str
将升级为unicode
(默认情况下使用编码ascii
)
另一方面,获得print
ed的unicode
实例将首先转换为str
,使用一种取决于打印流的编码(通常也是ascii
)
这些自动转换的情况通常是异常的来源(即如果转换失败)。如果捕捉到太多异常,这些异常可能会被忽略,然后某些功能就无法工作。Python与Unicode配合使用效果很好。以下是一篇综合文章: