The operation could not be completed. (Cocoa error: 3840.)

I am trying to parse JSON for an ios 6 application, but it can't seem to get it to work. I went through a lot of forums, but did not find a solution that works, which I understand enough to implement, or is it applicable.

I apologize if there is one that I missed.

First I have a test WebService which, as far as I can tell, returns a valid JSON

http://thetrouthunter.com/SVLocationsAPI.php

Secondly, here is my Objective-C code:

+ (NSDictionary *)connectToService:(NSString *)query { NSError *error = nil; query = [NSString stringWithFormat:@"%@&format=json&nojsoncallback=1", query]; query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil; NSLog(@"locations: %@", results); if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription); return results; } + (NSArray *)userLocation { NSString *request = [NSString stringWithFormat:@"http://thetrouthunter.com/SVLocationsAPI.php"]; return [[self connectToService:request] valueForKeyPath:@"locations.location"]; } 

The ls NSLog function displays an error message: "The operation could not be completed. (Cocoa error: 3840.)"

I can’t understand why this is so. I tried all kinds of things.

+8
json objective-c web-services ios6 nsjsonserialization
source share
1 answer

You add %@&format=json&nojsoncallback=1 to the url in connectToService: and this new url is displayed on the webpage, not the expected JSON (i.e. http://thetrouthunter.com/SVLocationsAPI.php&format= json & nojsoncallback = 1 ).

It might be useful to write the actual result from the HTTP request so that you can debug it until you get JSON (i.e. before calling serialization functions).

+4
source share

All Articles