我在通过 sbjson 将 iOS5 上的特殊字符发送到 Web 服务时遇到问题。我得到了错误:
-[NSNull isEqualToString:]: unrecognized selector sent to instance
我尝试发送不带特殊字符的 json 字符串,并且没有任何问题。但只要我输入一些特殊字符,如“æøå”或“é”,就会出现上述错误。
我在这里查看了很多关于 json 和 iOS 的答案,但大多数答案是关于 Web 服务的 URL 字符串中的特殊字符。我可以确定整个字符串也通过 SBJson 编码为 UTF-8。
任何人遇到过同样的问题,并且知道任何解决方案吗?
提前致谢。
向服务器发送请求时会发生这种情况。
这是我发送请求时使用的代码。
-(void) execMethodNSString*)methodName andParamsNSArray*)parameters withIDNSString*)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: @"OST"];
[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 _withConnectionAndDelegatenlyActive:]_block_invoke_0 + 40
7 Foundation 0x00eb7eb7 -[NSURLConnectionInternalConnection invokeForDelegate:] + 39
8 Foundation 0x00eb6e4f -[NSURLConnectionInternal _withConnectionAndDelegatenlyActive:] + 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
Best Answer-推荐答案 strong>
您的堆栈跟踪似乎只是可疑方法的堆栈跟踪,而不是发生错误的堆栈跟踪。见 Breaking on unrecognized selector了解如何设置断点以捕获确切位置。一旦它在断点处停止,您将获得包含更好信息的堆栈跟踪。
话虽如此,您的代码中有一行可疑:
NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];
这些行混合了字符串的长度(以字符为单位)和 UTF-8 编码的二进制数据的长度(以字节为单位)。一旦你有特殊字符(如带重音的字母),它们就会有所不同。
更好(更短)的替代方法是:
NSData* requestData = [reqString dataUsingEncoding:NSUTF8StringEncoding];
如果这不是解决方案,那么请添加改进的堆栈跟踪。
关于ios - Xcode/ios 5 到 SBJson 中的特殊字符,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/8637178/
|