我正在试图弄清楚如何从一个外部服务器的json文件中获取MWphotobrowser来获取照片,照片标题,照片缩略图等。
在viewDidLoad中,我有这段代码:
- (void)viewDidLoad {
NSURL *url = [NSURL URLWithString:@"https://s3.amazonaws.com/mobile-makers-lib/superheroes.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[super viewDidLoad];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSLog(@"Back from the web");
}];
NSLog(@"Just made the web call");
}
在Case3 MWphotobrowser的Menu.m中,我有以下代码:
case 3: {
photo.caption = [self.result valueForKeyPath:@"name"];
NSArray * photoURLs = [self.result valueForKeyPath:@"avatar_url"];
NSString * imageURL = [photoURLs objectAtIndex:indexPath.row];
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:imageURL]]];
enableGrid = NO;
break;
}
如果你错过了,我正在使用的JSON文件是https://s3.amazonaws.com/mobile-makers-lib/superheroes.json
我调整的任何内容似乎都无法实现,任何想法如何解决这个问题?
答案 0 :(得分:0)
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
// here make sure ur response is getting or not
if ([data length] >0 && connectionError == nil)
{
// DO YOUR WORK HERE
self.superheroes = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &connectionError];
NSLog(@"data downloaded.==%@", self.superheroes);
}
else if ([data length] == 0 && connectionError == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (connectionError != nil){
NSLog(@"Error = %@", connectionError);
}
}];
在这里你从服务器获得响应是NSArray -->
NSMutableDictionary` so
Case
方案是
case 3: {
NSDictionary *superhero = [self.superheroes objectAtIndex:indexPath.row];
photo.caption = superhero[@"name"];
NSString * imageURL = superhero[@"avatar_url"];
// NSArray * photoURLs = superhero[@"avatar_url"];
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:imageURL]]];
enableGrid = NO;
break;
}
你的最终出局是