[英]The Operation couldn't be completed. (Cocoa error: 3840.)
I am trying to parse JSON for an ios 6 app, but can't seem to get it to work. 我正在尝试为ios 6应用解析JSON,但似乎无法使其正常工作。 I have scoured tons of forums but haven't found a solution that works, that I understand enough to implement, or that applies.
我搜寻了很多论坛,但没有找到可行的解决方案,我了解足以实施的解决方案或适用的解决方案。
I apologize if there is one that I missed. 如果您错过了我,我深表歉意。
First I have a test WebService that as far as I can tell returns valid JSON 首先,我有一个测试WebService,据我所知返回有效的JSON
http://thetrouthunter.com/SVLocationsAPI.php http://thetrouthunter.com/SVLocationsAPI.php
Second, here is my Objective-C code: 其次,这是我的Objective-C代码:
+ (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 is printing out the error: "The operation couldn't be completed. (Cocoa error: 3840.)" ls NSLog函数正在打印错误:“操作无法完成。(可可错误:3840。)”
I can't figure out why this is the case. 我不知道为什么会这样。 I have tried all sorts of things.
我尝试了各种各样的事情。
You are adding %@&format=json&nojsoncallback=1
to the URL in connectToService:
, and that new URL results in a webpage, not the JSON you expect (ie http://thetrouthunter.com/SVLocationsAPI.php&format=json&nojsoncallback=1 ). 您将
%@&format=json&nojsoncallback=1
到connectToService:
的URL,并且该新URL会生成一个网页,而不是您期望的JSON(即http://thetrouthunter.com/SVLocationsAPI.php&format=json&nojsoncallback=1 )。
It might be useful to log the actual result from the HTTP request so that you can debug this until you get JSON (ie prior to calling the serialization functions). 记录来自HTTP请求的实际结果可能很有用,以便您可以调试它直到获得JSON(即在调用序列化函数之前)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.