2011-12-26 64 views
2

我在通過sbjson將iOS5上的特殊字符發送到Web服務時遇到問題。我得到的錯誤:Xcode/ios5中的特殊字符到SBJso

-[NSNull isEqualToString:]: unrecognized selector sent to instance

我試圖發送沒有特殊字符的json字符串,並且沒有任何問題。但只要我輸入一些特殊字符,如「æøå」或「é」,我就會得到上述錯誤。

我已經瀏覽了json和iOS上的很多答案,但大多數答案都是關於網絡服務的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

您可以添加錯誤的堆棧跟蹤以及創建Json數據並將其發送到問題的相關代碼。發送請求或接收答案時是否發生錯誤? – Codo 2011-12-26 16:04:29

+0

添加了您要求的信息。 :) – pvaskeli 2011-12-26 20:28:45

回答

2

看來你的堆棧跟蹤是suspicous方法只是堆棧跟蹤,但沒有發生的錯誤的堆棧跟蹤。請參閱Breaking on unrecognized selector瞭解如何設置斷點以捕捉確切位置。一旦它停在斷點上,你將得到一個信息更好的堆棧跟蹤。

話雖如此,有在代碼一個可疑行:

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

此線拌和字符串(在字符測量)的長度和UTF-8編碼的二進制數據的長度(以字節爲單位)。一旦你有像口音的字母這樣的特殊字符,它們就不一樣了。

一個更好的(較短)的替代方案是:

NSData* requestData = [reqString dataUsingEncoding:NSUTF8StringEncoding]; 

如果這不是解決辦法,那麼請加改進的堆棧跟蹤。

+0

這很好。謝謝你的幫助。 – pvaskeli 2011-12-27 16:51:59