Objective c JSON返回数据,但数据参数为nil

Objective c JSON返回数据,但数据参数为nil,objective-c,json,Objective C,Json,我正在处理一些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

我正在处理一些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:

当我将此URL插入web浏览器时,会得到格式化的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);
    }
}];
这是定义的
#定义WebServiceGetGlobalCores@”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");
    }
}];
}

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

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);
        }
    }];
已编辑 其他方法也行!!!见下文 现在我很惊讶为什么下面的代码可以工作而不是上面的代码

NSString*urlString=@

第三路, 较新的iOS模拟器版本>6.x出现一些问题。重置模拟器并检查代码。
作为参考,请转到
尝试使用
+dataWithContentsOfURL:options:error:
,这将为您提供一个
NSError
对象。另外,请注意,用户会说:

不要使用此同步方法请求基于网络的URL。对于 基于网络的URL,此方法可以阻止当前线程数十次 在慢速网络上的秒数,导致较差的用户体验,以及 在iOS中,可能导致您的应用程序终止

相反,对于非文件URL,请考虑使用 dataTaskWithURL:completionHandler:NSSession类的方法。看见 详细信息请参阅URL加载系统编程指南


使用异步下载数据NSData:dataWithContentsOfURL:不是一个好方法。什么NSLog(@“BOOM%s%@”,func,response);这一行返回你的数据?我从来没有看到这一行被记录。在我新创建的测试项目中,我得到了以下信息:BOOM uuu 29-[ViewController viewDidLoad]u block u invoke(null)参见问题谢谢您的帮助。urlRequest格式很好,但sendAsynchronousRequest似乎从未触发。我在日志中以及在“if”和“else”中都添加了log语句,但日志从未显示。不确定发生了什么。@bnjmn.myers您能用更新的代码更新您的问题吗??请参阅我的更新答案,我将另一个日志放置在读取有关响应的信息的位置。在你的问题中也更新了回答。我现在更新了。另一个有趣的小贴士是,我所有的应用程序都是在iOS 8和Xcode 6之前构建的,现在,每当我试图检索JSON时,它们都会抛出相同的错误。是不是发生了什么事情导致了JSON检索的重大变化?
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]);
}