简体   繁体   中英

RestKit POST not mapping response back to object

Now what I am trying to do is make a post to my /api/v1/login services, and then when i get the response back map that into a UserToken object.

Now I have the following code working for posting the UserLogin data, but its not mapping the responce back into an object:

- (void) postLogin
{

//Setup Login data
UserLogin *l = [[UserLogin alloc] initWithUsername:[usernameField text] andPassword:[passwordField text]];

//Setup Routes
RKObjectRouter *router = [RKObjectManager sharedManager].router;
[router routeClass:[UserLogin class] toResourcePath:@"/api/v1/login" forMethod:RKRequestMethodPOST];

//Setup Token Mapping
RKObjectMapping *UserTokenMapping = [RKObjectMapping mappingForClass:[UserToken class]];
[UserTokenMapping mapKeyPath:@"authentication_token" toAttribute:@"token"];
//[[[RKObjectManager sharedManager] mappingProvider]setMapping:UserTokenMapping forKeyPath:@""];
[[RKObjectManager sharedManager].mappingProvider registerMapping:UserTokenMapping withRootKeyPath:@""];

//Setup Mapping for UserLogin class
RKObjectMapping *UserLoginMapping = [RKObjectMapping mappingForClass:[UserLogin class]];
[UserLoginMapping mapKeyPath:@"login" toAttribute:@"userName"];
[UserLoginMapping mapKeyPath:@"password" toAttribute:@"passWord"];
[[RKObjectManager sharedManager].mappingProvider registerMapping:UserLoginMapping withRootKeyPath:@""];

[[RKObjectManager sharedManager] postObject:l delegate:self];

}

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
  NSLog(@"objects[%d]", [objects count]);
}

The response is coming back as:

{"message":"Login successful.","authentication_token":"2cksmLGr9hB5kLF3eRTu"}

The objectLoader method is just returning:

2012-09-26 23:49:57.046 voxpop_iphone[92118:c07] objects[0]

Can anyone help me with what I am missing here? Thanks heaps!!

I had the same problem and used the deprecated method to tune the right mapping

Instead

[[RKObjectManager sharedManager] postObject:l delegate:self];

I used

[objectManager postObject:self mapResponseWith:UserTokenMapping delegate:self];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM