PDA

View Full Version : stringEncooding iPhone App..




sujithkrishnan
Jun 27, 2008, 11:30 AM
Hi all..

In my app, i have a string say "Tony & Guy", i want to send it to the server through NSConnection....

I convert above string to NSData,and assigning it as HTTPBody...

But my server is throwing error as it parse "&" (the above string is a value for a node in an xml file)...

In java version, i converted the "&" to "%26" and POSTed it , working successfully..

Here also i tried with "%26" and "&"...Failed both times..

So on converting XMLstring to XMLData (NSData) using [myString -dataUsingStringEncoding:], which encoding i want to use???

I tried with NSUTF8Encoding and NSASCIIEncoding...

Still failed..

So my qstn is
tony %26 guy = [tony & guy stringUsingEncoding:????
Help....



Krevnik
Jun 27, 2008, 12:30 PM
It isn't an encoding issue. It is a URL escaping issue.

String encoding talks about the byte representation of the string (ASCII, UTF8, etc). Personally, I would do all network transfers of strings in UTF8 as it is the most compatible.

URL escaping is making sure that text can be made to not clash with the control characters used in URLs, and is independent of the string encoding.

You will probably want to write a small method that takes an NSString, and escapes it. I am not sure AppKit/Cocoa provides one for you.

Now, if we got a bit more information as to the problem (specifics), then we should be able to give a more specific answer.

Eraserhead
Jun 27, 2008, 01:32 PM
^^ You can use NSString's stringByReplacingPercentEscapesUsingEncoding: to remove spaces, and NSURL's standardizedURL may help.

sujithkrishnan
Jun 30, 2008, 12:34 AM
It isn't an encoding issue. It is a URL escaping issue.

String encoding talks about the byte representation of the string (ASCII, UTF8, etc). Personally, I would do all network transfers of strings in UTF8 as it is the most compatible.

URL escaping is making sure that text can be made to not clash with the control characters used in URLs, and is independent of the string encoding.

You will probably want to write a small method that takes an NSString, and escapes it. I am not sure AppKit/Cocoa provides one for you.

Now, if we got a bit more information as to the problem (specifics), then we should be able to give a more specific answer.


I worte a method which converts to convert symbols and spcl chars to escape codes, But Still it failing....

I am using UTF8 encoding only...

If i am pasting the same url in browser its working....

means i want to use some other method of NSURL or NSString for successfull URL creation...

What i am doing is ...

create a MutableRequest, init with a URL...

add an following string in NSData form as HTTPBody...


myChoice=
<category>
<sub> tony & guy</sub>
<sub> item 2 </sub>
<category>


I worte a method to convert it to..

myChoice=
<category>
<sub> tony %26 guy</sub>
<sub> item 2 </sub>
<category>


In both cases my app fail...
But in browser the second xml is a valid "POST"...where first is not..
But in my Iphone app, both are invalid...

sujithkrishnan
Jun 30, 2008, 07:46 AM
hi frnz..

everything worked fine with ....


stringByAddingPercentEscapesUsingEncoding ....

and my method to convert '&' to "%26"....

Thanks for thoughts...