1

我试图弄清楚如何让MWphotobrowser从外部服务器的 json 文件中获取照片、照片标题、照片缩略图等。

在 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

我没有任何调整似乎使它工作,任何想法如何解决这个问题?

4

1 回答 1

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` 所以

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;

    }

你的最终输出是

在此处输入图像描述

于 2014-09-06T07:10:09.217 回答