RestKit - POST response object mapping
I have method "getBuses" on my RESTful webservice (Rails). That method needs to return me list of buses in JSON. But, when I sending request for "/getBuses.json" I need to send
params = [NSDictionary
dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE",
@"fromSerbia", nil];
I'm posting params because I need filtered response (just for specific tour on specific date). Then, I expecting response like this:
{
"buses" : [ {
"bus_number" : "1",
"created_at" : "2011-08-15T23:07:52Z",
"id" : 1,
"lat" : 44.815,
"long" : 20.4665,
"model" : "Setra",
"registar_number" : "123456",
"seats" : 50,
"tour_id" : 1,
"updated_at" : "2011-08-15T23:07:52Z"
}, {
"bus_number" : "2",
"created_at" : "2011-08-15T23:07:52Z",
"id" : 2,
"lat" : 44.812,
"long" : 20.465,
"model" : "Mercedes",
"registar_number" : "2234",
"seats" : 60,
"tour_id" : 1,
"updated_at" : "2011-08-15T23:07:52Z"
} ]
}
I just need NSArray with Bus objects. Can you give me code example how to make t开发者_运维技巧his request and make object mapping with RestKit?
As I told you in the IRC Channel, in your RKClient instance you can use this method:
(NSURL *)URLForResourcePath:(NSString *)resourcePath queryParams:(NSDictionary *)queryParams
So you should do it like this:
[objectManager.mappingProvider setMapping:busesMapping forKeyPath:@"buses"];
params = [NSDictionary dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE", @"fromSerbia", nil];
NSURL *someURL = [objectManager.client URLForResourcePath:@"/service/getLocations.json" queryParams:params];
[objectManager loadObjectsAtResourcePath:[someURL absoluteString] delegate:self];
That method builds a valid URL using the resourcePath and the parameters you give as parameter. And then you need to set it back using the absoluteString of the NSURL object. Hope this helps!
精彩评论