stringEncooding iPhone App..

Discussion in 'iOS Programming' started by sujithkrishnan, Jun 27, 2008.

  1. macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    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....
     
  2. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #2
    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.
     
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    ^^ You can use NSString's stringByReplacingPercentEscapesUsingEncoding: to remove spaces, and NSURL's standardizedURL may help.
     
  4. thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #4

    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...

    Code:
    myChoice=
    <category>
    <sub> tony & guy</sub>
    <sub> item 2 </sub>
    <category>
    
    I worte a method to convert it to..
    Code:
    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...
     
  5. thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #5
    worked fine...

    hi frnz..

    everything worked fine with ....


    stringByAddingPercentEscapesUsingEncoding ....

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

    Thanks for thoughts...
     

Share This Page