Xcode / ios5到SBJso中的特殊字符

时间:2011-12-26 15:50:58

标签: ios json ios5 sbjson

我在iOS5上通过sbjson向Web服务发送特殊字符时遇到问题。我收到错误:

  

- [NSNull isEqualToString:]:无法识别的选择器发送到实例

我试图发送没有特殊字符的json字符串,它没有任何问题。但只要我输入一些特殊字符,如“æøå”或“é”,我就会收到上述错误。

我在这里看了很多关于json和iOS的答案,但主要是答案是关于Web服务的URL字符串中的特殊字符。我可以确定整个字符串也通过SBJson编码为UTF-8。

任何有同样问题的人,并知道有任何解决方法吗?

提前致谢。

将请求发送到服务器时会发生这种情况。

这是我发送请求时使用的代码。

-(void) execMethod:(NSString*)methodName andParams:(NSArray*)parameters withID:(NSString*)identificator {

//RPC
NSMutableDictionary* reqDict = [NSMutableDictionary dictionary];
[reqDict setObject:methodName forKey:@"method"];
[reqDict setObject:parameters forKey:@"params"];
[reqDict setObject:identificator forKey:@"id"];

//RPC JSON
NSString* reqString = [NSString stringWithString:[reqDict JSONRepresentation]];

//Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];

//prepare http body
[request setHTTPMethod: @"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];

if (urlConnection != nil) {
    [urlConnection release];
    urlConnection = nil;
}

urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[request release];

UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;

}

堆栈跟踪:

0   xRapp                               0x00015925 -[JSONRPCService execMethod:andParams:withID:] + 1077
1   xRapp                               0x00017eaa -[xRappSyncWSViewController syncServerUpdate:] + 2586
2   xRapp                               0x000187a7 -[xRappSyncWSViewController data1Loaded:] + 2167
3   xRapp                               0x000171d3 -[xRappSyncWSViewController dataLoaded:] + 627
4   xRapp                               0x00015da8 -[JSONRPCService connectionDidFinishLoading:] + 200
5   Foundation                          0x00eb8a59 ___NSURLConnectionDidFinishLoading_block_invoke_0 + 40
6   Foundation                          0x00eb6e94 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke_0 + 40
7   Foundation                          0x00eb7eb7 -[NSURLConnectionInternalConnection invokeForDelegate:] + 39
8   Foundation                          0x00eb6e4f -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 201
9   Foundation                          0x00eb6fd5 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 76
10  Foundation                          0x00dfbf6a _NSURLConnectionDidFinishLoading + 43
11  CFNetwork                           0x01998bbd _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 241
12  CFNetwork                           0x01a655ea _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 584
13  CFNetwork                           0x0198f298 _ZN19URLConnectionClient13processEventsEv + 174
14  CFNetwork                           0x01a6516b _ZThn52_N25URLConnectionInstanceData24multiplexerClientPerformEv + 21
15  CFNetwork                           0x0198f137 _ZN17MultiplexerSource7performEv + 259
16  CoreFoundation                      0x0120b97f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
17  CoreFoundation                      0x0116eb73 __CFRunLoopDoSources0 + 243
18  CoreFoundation                      0x0116e454 __CFRunLoopRun + 1012
19  CoreFoundation                      0x0116ddb4 CFRunLoopRunSpecific + 212
20  CoreFoundation                      0x0116dccb CFRunLoopRunInMode + 123
21  GraphicsServices                    0x01eef879 GSEventRunModal + 207
22  GraphicsServices                    0x01eef93e GSEventRun + 114
23  UIKit                               0x004c3a9b UIApplicationMain + 1175
24  xRapp                               0x000021f8 main + 152
25  xRapp                               0x00002155 start + 53

1 个答案:

答案 0 :(得分:2)

您的堆栈跟踪似乎只是可疑方法的堆栈跟踪,而不是发生错误的堆栈跟踪。有关如何设置断点以捕获确切位置的信息,请参阅Breaking on unrecognized selector。一旦它在断点处停止,就会得到一个包含更好信息的堆栈跟踪。

话虽如此,您的代码中有一条可疑行:

NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];

这一行混合了字符串的长度(以字符为单位)和UTF-8编码的二进制数据的长度(以字节为单位)。一旦你有特殊字符,如带有重音的字母,它们就会有所不同。

更好(和更短)的替代方案是:

NSData* requestData = [reqString dataUsingEncoding:NSUTF8StringEncoding];

如果那不是解决方案,那么请添加改进的堆栈跟踪。