简体   繁体   English

该操作无法完成。 (可可错误:3840。)

[英]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=1connectToService:的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.

相关问题 错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(可可错误3840.) - Error Domain = NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.) 如何解决错误:操作无法完成。 (可可错误3840。)json iOS? - How to resolve error:the operation couldn’t be completed. (cocoa error 3840.) json iOS? iOS - Twitter连接问题:“操作无法完成。 (可可错误3840.)“ - iOS - Twitter Connectivity Issue: “The operation couldn’t be completed. (Cocoa error 3840.)” 该操作无法完成。 (可可错误3840。)”(在字符16周围的对象中没有键值。) - The operation couldn’t be completed. (Cocoa error 3840.)" (No value for key in object around character 16.) JSON NSJSONSerialization无法完成该操作。 (可可错误3840。) - JSON NSJSONSerialization The operation couldn’t be completed. (Cocoa error 3840.) JSON解析错误:错误域= NSCocoaErrorDomain代码= 3840“该操作无法完成。(可可错误3840。) - JSON parse error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.) 该操作无法完成。 可可错误4 - The operation couldn’t be completed. Cocoa Error 4 “这项行动无法完成。 (可可错误512.)“ - “The operation couldn’t be completed. (Cocoa error 512.)” 该操作无法完成。 (可可错误1560.) - The operation couldn’t be completed. (Cocoa error 1560.) 无法保存到数据存储:无法完成操作。 (可可错误133020.) - Failed to save to data store: The operation couldn’t be completed. (Cocoa error 133020.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM