【发布时间】: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 插入网络浏览器时,我会得到格式化的 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