我开发了一个iPhone应用程序,我需要将一个字符串数组传递给Web服务。
现在,我有一个NSMutable
数组,比如sendSelectedID
。
我知道我在数组中有值,因为当我在for循环中显示时
for(i=0;i<[sendSelectedID count];i++)
{
NSLog(@"%@", [sendSelectedID objectAtindex:i]);
}
这是我将NSMutable
数组传递给Soap消息的方式:
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<S:Header/\>\n"
"<S:Body>\n"
"<ns2:confirmRecallDeposit xmlns:ns2=\"http://services.cbp.syntel.org/\">\n"
"<TransactionId>%@</TransactionId>\n"
"</ns2:confirmRecallDeposit>\n"
"</S:Body>\n"
"</S:Envelope>\n",sendSelectedID ];
,肥皂信息显示如下:
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<S:Header/\>\n"
"<S:Body>\n"
"<ns2:confirmRecallDeposit xmlns:ns2=\"http://services.cbp.syntel.org/\">\n"
"<TransactionId>(
1,
3
)</TransactionId>\n"
"</ns2:confirmRecallDeposit>\n"
"</S:Body>\n"
"</S:Envelope>\n",sendSelectedID ];
但是在服务器端,它给出了错误的数字格式异常。 我如何在服务器端删除此错误?我试图修剪收到的值,但它没有用。
答案 0 :(得分:0)
这只是一个猜测,但也许你需要在sendSelectedID
中逃避回车。
您可以将方法-stringByEscapingMetacharacters
添加到NSString
类别(将功能扩展到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;
}
然后,您可以致电[sendSelectedID stringByEscapingMetacharacters]
获取以下NSString*
:
(\\n1,\\n3\\n)
而不是现在拥有的东西:
(
1,
3
)