2013-03-09 2 views
0

나는 SMS 메시지를 읽고 Google 애플 리케이션 엔진 서버로 보내는 안드로이드 애플리케이션이있다. 일부 사용자는 특정 언어가 제대로 전달되지 않는다고 불평하고 있습니다.자바와 파이썬의 유니 코드 문자열 지원

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

메시지 변수에 다른 언어의 SMS 메시지가 포함되어 있습니다. 어떻게 지원하나요? 또한, 그것을 내 서버 (파이썬)로 보내야하며, 어떻게하면 유니 코드를 서버에서 번역 할 수 있습니까?

+0

파이썬은 유니 코드와 잘 호환됩니다. 다음은 포괄적 인 기사입니다. http://docs.python.org/2/howto/unicode.html – jyore

답변

1

파이썬 2.7에는 str (바이트로 구성된 표준 문자열)과 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 

변환은 종종 암시 적으로 발생합니다. 지. unicodestr을 추가하면 연결하기 전에 strunicode (인코딩 ascii을 사용하여 기본적으로)으로 승격됩니다. 한편

, print 에드이 (통상적 ascii뿐만 아니라)에 인쇄 도착 스트림에 의존하는 인코딩을 사용하여, 먼저 str로 변환한다 얻는 unicode 인스턴스.

이러한 자동 변환은 종종 예외 (예 : 변환이 실패한 경우)의 원인입니다. 지나치게 많은 예외를 잡으면, 이것들은 눈에 띄지 않을 수 있으며, 그런 다음 단지 일부 기능 만 작동하지 않습니다.