Special characters in Xcode/ios5 to SBJso

2

I am having a problem with sending special characters on iOS5 through sbjson to a web service. I get the error:

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

I have tried to send the json string without special characters, and it went through without any problems. But as soon as I enter some special characters like "æøå" or "é" I get the error above.

I have looked through a lot of answers on json and iOS here, but mostly the answers is about special characters in the URL-string to the web service. For what I can determine the whole string is also encoded to UTF-8 through SBJson.

Anyone that have had the same problem, and know of any solutions for this?

Thanks in advance.

This happens when sending the request to the server.

This is the code I use when I am sending the request.

-(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;

}

The stack trace:

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
ios
json
ios5
sbjson
asked on Stack Overflow Dec 26, 2011 by pvaskeli • edited Dec 26, 2011 by pvaskeli

1 Answer

2

It seems that your stacktrace is just the stacktrace of the suspicous method but not the stack trace of the error that occurs. See Breaking on unrecognized selector for how to set a breakpoint to catch the exact location. Once it stops on the breakpoint, you get a stacktrace with better information.

Having said that, there is one suspicious line in your code:

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

This lines mixes up the length of the string (measured in characters) and the length of the UTF-8 encoded binary data (measured in bytes). They are different once you have special characters like letters with accents.

A better (and shorter) alternative is:

NSData* requestData = [reqString dataUsingEncoding:NSUTF8StringEncoding];

If that's not the solution, then please add the improved stacktrace.

answered on Stack Overflow Dec 26, 2011 by Codo • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0