JSON返回数据但数据参数为nil

时间:2015-02-02 04:54:05

标签: 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 *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];

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

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

我得到的数据参数是nil'。

任何想法我做错了什么?

更新:

我现在正在使用:

// 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");
    }
}];
}

2 个答案:

答案 0 :(得分:0)

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

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模拟器版本&gt; 6.x有一些问题。重置您的模拟器并检查您的代码。 如需参考,请转到NSURLConnection GET request returns -1005, "the network connection was lost"

答案 1 :(得分:0)

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

  

请勿使用此同步方法来请求基于网络的网址。对于   基于网络的URL,此方法可以阻止当前线程为数十   慢速网络上的秒数,导致用户体验不佳,以及   在iOS中,可能会导致您的应用被终止。

     

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