[android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Alok Kulkarni
Hi,
I am having a unicode string \u3403 which is actualy some japansee character
I want to pass it through a JSON object. So i put the value as say
String str =  \u3403
jsonObject.put(name,str);
When i do this  the json object internally adds another escape
sequence as  \\u3403, and the request string has two \ slashes.
This is interpreted wrongly by the server as it does not detect
unicode name.
What can i do for this ?
Thanks,
Alok.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Daniel Drozdzewski
On Fri, Nov 12, 2010 at 3:01 PM, Alok Kulkarni kulsu...@gmail.com wrote:
 Hi,
 I am having a unicode string \u3403 which is actualy some japansee character
 I want to pass it through a JSON object. So i put the value as say
 String str =  \u3403
 jsonObject.put(name,str);
 When i do this  the json object internally adds another escape
 sequence as  \\u3403, and the request string has two \ slashes.
 This is interpreted wrongly by the server as it does not detect
 unicode name.
 What can i do for this ?
 Thanks,
 Alok.

Alok,

\u3403 is not a unicode string, but a string that has hex code of an
unicode character in it. It can be unicode encoded or its encoding can
be anything else really.
Since \ is a special character and has extra meaning in Java String
class, it gets escaped by escape character, which is \. Now you
know, what is the extra meaning of \  ;-)

Now JSON should not have any problems with parsing such
character/string, even when double escaped. Make sure that your server
is correctly configured for use of unicode.
Also try sending the actual character (paste it from some place that
generates a character based on its hex code), but please make sure
that your source code is unicode encoded, otherwise it will not work.

Daniel

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Alok Kulkarni
Thanks Daniel for your response,
In my app, the user enters a name in japanese language which is to be
sent to server in a JSON request.The server expects the name parameter
in form of a UTF-8 encoded string.
So the encoding of source code should not matter.Here is the sample code


 public static String EncodeJson(String strJson)
 {
 try
 {
 String strHex = null;
 for (int i = 0; i  strJson.length(); i++)
 {
 try{
 //char c = strJson.charAt(i);
// int j = (int) c;
// System.out.println(ASCII OF +c + =  + j + .);
 //System.out.println(strJson.charAt(i), 10=
+Integer.parseInt(+strJson.charAt(i), 10));
 if ( strJson.charAt(i) = 126)
 {
 String start = strJson.substring(0, i);
 String end = strJson.substring(i + 1);


 int jsonChar = strJson.charAt(i); // new
string(strJson[i], 1);
 strHex = 000 + Integer.toHexString(jsonChar);
 strHex = strHex.toUpperCase();

 strJson = start + \\u +
strHex.substring(strHex.length()-4)+ end;
 i += 5;
 }
 }catch(Exception e){
  System.out.println( Exception -
strJson.charAt(i)= +strJson.charAt(i));
 }
 }
 return strJson;
 }
 catch (Exception ex)
 {
 Log.i(Encode, ex.toString());
 return strJson;
 }
 }

The output of this method is passed to JSON object using its put
method.At this point an additional  \ gets added to the parameter
due to which the server does not recognize this as a valid UTF string
Thanks,
Alok.

On Fri, Nov 12, 2010 at 9:07 PM, Daniel Drozdzewski
daniel.drozdzew...@gmail.com wrote:
 On Fri, Nov 12, 2010 at 3:01 PM, Alok Kulkarni kulsu...@gmail.com wrote:
 Hi,
 I am having a unicode string \u3403 which is actualy some japansee 
 character
 I want to pass it through a JSON object. So i put the value as say
 String str =  \u3403
 jsonObject.put(name,str);
 When i do this  the json object internally adds another escape
 sequence as  \\u3403, and the request string has two \ slashes.
 This is interpreted wrongly by the server as it does not detect
 unicode name.
 What can i do for this ?
 Thanks,
 Alok.

 Alok,

 \u3403 is not a unicode string, but a string that has hex code of an
 unicode character in it. It can be unicode encoded or its encoding can
 be anything else really.
 Since \ is a special character and has extra meaning in Java String
 class, it gets escaped by escape character, which is \. Now you
 know, what is the extra meaning of \  ;-)

 Now JSON should not have any problems with parsing such
 character/string, even when double escaped. Make sure that your server
 is correctly configured for use of unicode.
 Also try sending the actual character (paste it from some place that
 generates a character based on its hex code), but please make sure
 that your source code is unicode encoded, otherwise it will not work.

 Daniel

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Frank Weiss

 \u3403 is not a unicode string, but a string that has hex code of an
 unicode character in it. It can be unicode encoded or its encoding can
 be anything else really.
 Since \ is a special character and has extra meaning in Java String
 class, it gets escaped by escape character, which is \. Now you
 know, what is the extra meaning of \  ;-)

 This is not correct in Java. Strings are stored internally as UCS-16.
Therefore, \u3403.length() == 1. However, \\u3403.length() == 6. This
can be verified with a simple Java program.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Frank Weiss
It looks like the code you are using is adding the extra \ here:

strJson = start + \\u file://u/ +
strHex.substring(strHex.length()-4)+ end;

I don't understand the need for the EncodeJson function. As I pointed out
earlier, Java stores char and String data internally as sequences of 16-bit
character codes (UCS-16).

What JSON library are you using to encode the data object you are sewnding
to the server?

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Alok Kulkarni
I am using Androids JSON library,
The extra \ in the line

strJson = start + \\u + strHex.substring(strHex.length()-4)+ end;

has to be  added as an escape sequence else the code does not compile.

I don't understand the need for the EncodeJson function.
-Though internally java might be storing as UCS-16, when passing to
the web service , the web service expectes the parameter to be in
UTF-8 encoded string.
Thanks,
Alok.
On Fri, Nov 12, 2010 at 9:42 PM, Frank Weiss fewe...@gmail.com wrote:
 \u3403 is not a unicode string, but a string that has hex code of an
 unicode character in it. It can be unicode encoded or its encoding can
 be anything else really.
 Since \ is a special character and has extra meaning in Java String
 class, it gets escaped by escape character, which is \. Now you
 know, what is the extra meaning of \  ;-)

 This is not correct in Java. Strings are stored internally as UCS-16.
 Therefore, \u3403.length() == 1. However, \\u3403.length() == 6. This
 can be verified with a simple Java program.


 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Daniel Drozdzewski
Alok,

On Fri, Nov 12, 2010 at 3:56 PM, Alok Kulkarni kulsu...@gmail.com wrote:
 Thanks Daniel for your response,
 In my app, the user enters a name in japanese language which is to be
 sent to server in a JSON request.The server expects the name parameter
 in form of a UTF-8 encoded string.

In JSON you can pass an actual UTF-8 encoded char, like '®' or its
safe form (some call it ASCII form or URL encoded form) like '\u00AE'
I suggested that you can test few things, for example sending an
actual character, but then you have to make sure that your source code
is UTF-8 encoded, since the literal is used directly, therefore has to
be UTF-8 encoded.

There are know issues with some PHP installations running on Win
servers that use CP1251 encoding, so if your SOAP receiver is a PHP
script in such setup, it would inherit the encoding from PHP.

Again, the way to test your server's correctness, you can try passing
the actual character rather than its code, BUT make sure that the
source is UTF-8 encoded, for the reasons stated above.


Daniel

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Frank Weiss
Yes it won't compile, but I think you are supposing that (\\u file://u/
+ 3403) is the same as \u3403. At any rate, I still thing the EncodeJson
function is bogus.

AFAIK, the JSON encoder should take care of the UTF-8 encoding. It could
also use JSON encoding.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Problem passing Unicode string through JSON request

2010-11-12 Thread Daniel Drozdzewski
On Fri, Nov 12, 2010 at 4:33 PM, Frank Weiss fewe...@gmail.com wrote:
 Yes it won't compile, but I think you are supposing that (\\u + 3403) is
 the same as \u3403. At any rate, I still thing the EncodeJson function is
 bogus.

 AFAIK, the JSON encoder should take care of the UTF-8 encoding. It could
 also use JSON encoding.

+1

The call, we are looking for is:
public static String quote (String data) in class JSONObject.

Daniel

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en