2013-02-26 217 views
0

我有一个生成位置的轨道API,以及这些位置的菜单。在我的iOS应用程序中,我使用AFNetworking在表格视图中检索这些位置的json列表。AFNetworking嵌套参数请求

这是AFNetworking要求我用得到的位置数据

LocationTableViewController.m

[[LocationApiClient sharedInstance] getPath:@"locations.json" parameters:nil 
             success:^(AFHTTPRequestOperation *operation, id response) { 
              NSLog(@"Response: %@", response); 
              NSMutableArray *results = [NSMutableArray array]; 
              for (id locationDictionary in response) { 
               Location *location = [[Location alloc] initWithDictionary:locationDictionary]; 
               [results addObject:location]; 

              } 
              self.results = results; 
              [self.tableView reloadData]; 
             } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
              NSLog(@"Error fetching locations!"); 
              NSLog(@"%@", error); 

             }]; 

的JSON的样品返回的位置请求

{ 
    "created_at" = "2013-02-23T19:25:26Z"; 
    id = 2; 
    lat = "48.870175"; 
    long = "2.779899999999998"; 
    name = "Disneyland Hotel"; 
    "places_id" = fd90769e2cf40d6cb6cb1a3d0bfd0ce5d37ed331; 
    "street_address" = "Rue de la Marni\U00e8re, Chessy, France"; 
    "updated_at" = "2013-02-23T19:25:26Z"; 
}, 

名称和地址显示

enter image description here

当用户点击位置单元格时,我想显示该位置啤酒列表的表格视图。导轨API路线(用于LOCATION_ID => 1)是http://localhost:3000/locations/1/beers.json和输出以下JSON为属于LOCATION_ID啤酒:1

{ 
"created_at":"2013-02-23T19:25:53Z", 
"description":"fkjgvjkg", 
"id":1,"location_id":1, 
"name":"nhoihioh", 
"price":"2.5","style":"ipa", 
"updated_at":"2013-02-23T19:25:53Z" 
    } 

当用户点击的位置处小区我想显示的是,啤酒列表位置,通过http://localhost:3000/locations/(whatever location_id is associated with the table cell that was tapped)/beers.json

消耗如何将location_id插入到下一个表视图中发生的AFNetworking请求中,当用户点击位置单元格时?

BeerTableViewController.m

[[LocationApiClient sharedInstance] getPath:@"locations/**(location_id from location table cell inserted here)**/beers.json" parameters:nil 
             success:^(AFHTTPRequestOperation *operation, id response) { 
              NSLog(@"Response: %@", response); 
              NSMutableArray *results = [NSMutableArray array]; 
              for (id beerDictionary in response) { 
               Beer *beer = [[Beer alloc] initWithDictionary:beerDictionary]; 
               [results addObject:beer]; 

              } 
              self.results = results; 
              [self.tableView reloadData]; 
             } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
              NSLog(@"Error fetching beers!"); 
              NSLog(@"%@", error); 

             }]; 

我想我需要登录的LOCATION_ID并传递价值,当表格单元格被点击的URL设置了一个param,但我不知道该怎么做。

回答

1

在使用JSON时,您应该使用AFJSONRequestOperation,因为它会将响应数据解析为字典。

AFJSONRequestOperation JSONRequestOperationWithRequest:request 
                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id locationJSON) { 

.... 

}; 

然后,您可以从字典中获取places_id,然后再打电话来获取啤酒信息。