سؤال

I'm trying to learn AFNetworking so have written a simple block. I'm trying to retrieve & log the json from the site url below.

NSString *string =
@"http://transportapi.com/v3/uk/bus/stop/490012745J/live.json?api_key=6ee115459cbeccdb902b14d39b61330d&app_id=9deefeb1&group=route";
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSDictionary *mydict = (NSDictionary *)responseObject;

     NSString *key;
     for(key in mydict){
         NSLog(@" key %@", key);
     }

 }
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                     NSLog(@"ERROR");
                                 }];

But neither the success or the failure block is being called. Can someone point out what I've done wrong?

هل كانت مفيدة؟

المحلول

You're not actually firing the operation you need to add

[[NSOperationQueue mainQueue] addOperation:operation];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top