![](/img/trans.png)
[英]Error Domain = NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 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=1
到connectToService:
的URL,並且該新URL會生成一個網頁,而不是您期望的JSON(即http://thetrouthunter.com/SVLocationsAPI.php&format=json&nojsoncallback=1 )。
記錄來自HTTP請求的實際結果可能很有用,以便您可以調試它直到獲得JSON(即在調用序列化函數之前)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.