AFNetworking JSON请求,既不成功也不调用错误块

时间:2014-04-22 08:47:04

标签: objective-c json nsdictionary afnetworking

我正在尝试学习AFNetworking,所以写了一个简单的块。我正在尝试检索&从下面的网站URL记录json。

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");
                                 }];

但是,成功或失败块都没有被调用。有人可以指出我做错了吗?

1 个答案:

答案 0 :(得分:1)

您实际上并未触发添加

所需的操作
[[NSOperationQueue mainQueue] addOperation:operation];