我有一个rails api,可以为这些位置生成位置和菜单。在我的iOS应用程序中,我正在使用AFNetworking在表格视图中检索这些位置的json列表。
这是我用来获取位置数据的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";
},
显示名称和地址
当用户点击位置单元格时,我想显示该位置啤酒列表的表格视图。 rails api路由(对于location_id => 1)为http://localhost:3000/locations/1/beers.json
,并为属于location_id的啤酒输出以下json: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的参数传递,但我不知道该怎么做。
答案 0 :(得分:1)
当您使用JSON时,您应该使用AFJSONRequestOperation,因为它会将响应数据解析为字典。
AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id locationJSON) {
....
};
然后,您可以从字典中获取places_id并再次拨打电话获取啤酒信息。