HalesEnchanted HalesEnchanted - 5 months ago 24
Objective-C Question

MWphotobrowser - Fetch Images from Json

I'm trying to figure out how to get MWphotobrowser to fetch photo, photo caption, photo thumbnail etc. from a json file an extermal server.

In viewDidLoad, I have this code:

- (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");

}


In Case3 MWphotobrowser's Menu.m, I have the following code:

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;

}


Incase you missed it, the JSON file I'm using is https://s3.amazonaws.com/mobile-makers-lib/superheroes.json

Nothing I tweak seems to make it work, any ideas how to fix this?

Answer
[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);
     }



   }];

in here u r getting the response from server is NSArray -->NSMutableDictionary` so

Case scenario is

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;

    }

ur final out put is

enter image description here

Comments