【问题标题】:JSON returning data but data parameter is nilJSON返回数据但数据参数为零
【发布时间】:2015-04-01 00:29:00
【问题描述】:

我弄乱了一些 API 的东西并尝试了以下方法:

#define searchWebService @"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b667841296224aab0371a6f4a4546662&format=json&per_page=20&page=1"

// Construct url string for search
NSString *urlString = [NSString stringWithFormat:@"%@&text=%@&nojsoncallback=1", searchWebService, keyword];
//NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Create url via formatted string
NSURL *url = [NSURL URLWithString:urlString];

// Get all data from the return of the url
NSData *photoData = [NSData dataWithContentsOfURL:url];

// Place all data into a dictionary
NSDictionary *allData = [NSJSONSerialization JSONObjectWithData:photoData options:kNilOptions error:nil];

这是构建的 URL: https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b667841296224aab0371a6f4a4546662&format=json&per_page=20&page=1&text=ball&nojsoncallback=1

当我将此 URL 插入网络浏览器时,我会得到格式化的 JSON,但是当我尝试将其插入:NSData *photoData = [NSData dataWithContentsOfURL:url];

我得到'数据参数为零'。

任何想法我做错了什么?

更新:

我现在正在使用:

// Construct url string for search
NSString *urlString = [NSString stringWithFormat:@"%@&text=%@&nojsoncallback=1", searchWebService, keyword];

NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

[NSURLConnection sendAsynchronousRequest:theRequest queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSLog(@"BOOM %s %@", __func__, response);
    if(!connectionError){
}
    else{
        NSLog(@"%s %@", __func__, connectionError.localizedDescription);
    }

但这些日志都不会显示在控制台中。

更新:测试项目 我创建了一个全新的项目并将以下代码放入 viewDidLoad 方法中:

NSString *urlString = [NSString stringWithFormat:@"%@", webServiceGetGlobalScores];

NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:formattedURLString]];
NSLog(@"theRequest: %@", theRequest);
[NSURLConnection sendAsynchronousRequest:theRequest queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSLog(@"BOOM %s %@", __func__, response);
    if(!connectionError){
    }
    else{
        NSLog(@"%s %@", __func__, connectionError.localizedDescription);
    }
}];

这是定义的#define webServiceGetGlobalScores @"http://www.appguys.biz/JSON/iTapperJSON.php?key=weBeTappin&method=getGlobalScores"

但 sendAsynchronousRequest 仍然没有记录任何内容。

更新 3

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Construct url string for search
NSString *urlString = [NSString stringWithFormat:@"%@", webServiceGetGlobalScores];
NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSLog(@"urlRequest: %@", urlRequest);

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {NSLog(@"BOOM %s %@", __func__, response);
     NSLog(@"ERROR: %@", error);
    if ([data length] > 0 && error == nil){
        NSLog(@"BOOM");
    }
}];
}

【问题讨论】:

  • 使用异步下载数据 NSData:dataWithContentsOfURL: 不是好办法。
  • 什么 NSLog(@"BOOM %s %@", func, response);此行返回您的数据?
  • 我从未看到该行被记录。就好像它根本没有进入那个街区。
  • 在我新创建的测试项目中,我得到了这个:BOOM __29-[ViewController viewDidLoad]_block_invoke (null)

标签: objective-c json


【解决方案1】:

您可以使用此代码下载数据,

NSString *urlString = //your whatever URL
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

    [NSURLConnection sendAsynchronousRequest:theRequest queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSLog(@"%s %@", __func__, response);
        if(!connectionError){
            //Parse your JSON data
        }
        else{
            NSLog(@"%s %@", __func__, connectionError.localizedDescription);
        }
    }];

已编辑 其他方法有效!!! o_O 见下文 现在我很惊讶为什么下面的代码工作而不是上面的代码。

NSString *urlString = @"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b667841296224aab0371a6f4a4546662&format=json&per_page=20&page=1&text=ball&nojsoncallback=1";

NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSError *theErr;
NSData *thd = [NSData dataWithContentsOfURL:[NSURL URLWithString:formattedURLString] options:0 error:&theErr];
if(theErr){
    NSLog(@"%@", theErr.localizedDescription);
}
else{
    NSLog(@"%@", [[NSString alloc] initWithData:thd encoding:NSUTF8StringEncoding]);
}

第三种方式, 较新的 iOS 模拟器版本 > 6.x 有一些问题。重置您的模拟器并检查您的代码。 供参考去NSURLConnection GET request returns -1005, "the network connection was lost"

【讨论】:

  • 感谢您的帮助。 urlRequest 格式很好,但 sendAsynchronousRequest 似乎永远不会触发。我已经在其中以及“if”和“else”中添加了一个日志语句,但日志从未出现。不知道发生了什么。
  • @bnjmn.myers 你能用更新的代码更新你的问题吗?查看我更新的答案我放置了另一个关于响应的日志阅读信息。在您的问题中也更新了该回复。
  • 我已经更新了。另一个有趣的消息是,我拥有的所有应用程序都是在 iOS 8 和 Xcode 6 之前构建的,现在每当我尝试检索 JSON 时它们都会抛出相同的错误。是否发生了导致检索 JSON 发生重大变化的事情?
【解决方案2】:

尝试改用+ dataWithContentsOfURL:options:error:,它会为您提供NSError 对象。另外,请注意docs 说:

不要使用这种同步方法来请求基于网络的 URL。为了 基于网络的URL,该方法可以阻塞当前线程数十 在慢速网络上的秒数,导致糟糕的用户体验,以及 在 iOS 中,可能会导致您的应用被终止。

相反,对于非文件 URL,请考虑使用 dataTaskWithURL:completionHandler: NSSession 类的方法。看 URL 加载系统编程指南了解详情。

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2019-05-01
    • 2014-02-14
    相关资源
    最近更新 更多