2013-02-26 212 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,然後再打電話來獲取啤酒信息。