tags:

views:

55

answers:

1

i have developed an iphone application, where i need to pass a string array to the web service now, i have a NSMutable array say sendSelectedID

i know, i have values in the array, because when i display in for loop for(i=0;i<[sendSelectedID count];i++) { NSLog(@"%@", [sendSelectedID objectAtindex:i]); }

this is the way i pass the NSMutable array to the Soap message

NSString *soapMessage = [NSString stringWithFormat:
   @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
   "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;\n"
   "<S:Header/\>\n"
   "<S:Body>\n"
   "<ns2:confirmRecallDeposit xmlns:ns2=\"http://services.cbp.syntel.org/\"&gt;\n"
   "<TransactionId>%@</TransactionId>\n"
   "</ns2:confirmRecallDeposit>\n"
   "</S:Body>\n"
   "</S:Envelope>\n",sendSelectedID ];

and the soap message is displyed like

NSString *soapMessage = [NSString stringWithFormat:
  @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
 "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;\n"
  "<S:Header/\>\n"
  "<S:Body>\n"
  "<ns2:confirmRecallDeposit xmlns:ns2=\"http://services.cbp.syntel.org/\"&gt;\n"
  "<TransactionId>(
        1,
        3
      )</TransactionId>\n"
  "</ns2:confirmRecallDeposit>\n"
  "</S:Body>\n"
  "</S:Envelope>\n",sendSelectedID ];

But on the server side, it gives error Number format exception. Please help me, how am i to remove this error, on the server side, i tried to trim the values recieved, but it did not work.

A: 

This is just a guess, but perhaps you need to escape the carriage returns in sendSelectedID.

You could add the method -stringByEscapingMetacharacters to an NSString category (a way to extend functionality to NSString):

@implementation NSString (Additions)

- (NSUInteger) numberOfUnsafeCharacters {
    NSUInteger i, len, cnt;

    if ((len = [self length]) == 0)
        return 0;

    for (i = 0, cnt = 0; i < len; i++) {
        unichar c;

        c = [self characterAtIndex:i];
        if (c == ',' || c == ';' || c == '\n' || c == '\\') cnt++;
    }
    return cnt;
}

- (NSString *) stringByEscapingMetacharacters {
    NSUInteger i, cnt, len;
    unichar  *newStr;
    NSString *result;

    if ((cnt = [self numberOfUnsafeCharacters]) == 0)
        return self;

    len = [self length];
    newStr = calloc(len + cnt + 10, sizeof(unichar));
    for (i = 0, cnt = 0; i < len; i++) {
        unichar c;

        c = [self characterAtIndex:i];
        if (c == '\\') {
            newStr[i + cnt] = '\\';
            cnt++;
        }
        else if (c == '\n') {
            newStr[i + cnt] = '\\';
            cnt++;
            c = 'n';
        }
        newStr[i + cnt] = c;
    }
    newStr[i + cnt] = '\0';
    result = [NSString stringWithCharacters:newStr length:(i + cnt)];
    if (newStr != NULL) free(newStr); newStr = NULL;

    return result;
}

Then you would call [sendSelectedID stringByEscapingMetacharacters] to get the following NSString*:

(\\n1,\\n3\\n)

Instead of what you have now:

(
1,
3
)
Alex Reynolds