繁体   English   中英

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

[英]The Operation couldn't be completed. (Cocoa error: 3840.)

我正在尝试为ios 6应用解析JSON,但似乎无法使其正常工作。 我搜寻了很多论坛,但没有找到可行的解决方案,我了解足以实施的解决方案或适用的解决方案。

如果您错过了我,我深表歉意。

首先,我有一个测试WebService,据我所知返回有效的JSON

http://thetrouthunter.com/SVLocationsAPI.php

其次,这是我的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"];
}

ls NSLog函数正在打印错误:“操作无法完成。(可可错误:3840。)”

我不知道为什么会这样。 我尝试了各种各样的事情。

您将%@&format=json&nojsoncallback=1connectToService:的URL,并且该新URL会生成一个网页,而不是您期望的JSON(即http://thetrouthunter.com/SVLocationsAPI.php&format=json&nojsoncallback=1 )。

记录来自HTTP请求的实际结果可能很有用,以便您可以调试它直到获得JSON(即在调用序列化函数之前)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM