当我像这样
//Query Parameters for Login: Email and Password
NSDictionary *queryParams;
queryParams = [NSDictionary dictionaryWithObjectsAndKeys: email,@"email",password,@"password", nil];
RKObjectManager *objectManager = [RKObjectManager sharedManager];
//Pointing to the login query in the server
RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:@"/default/api/login.json" queryParameters:queryParams];
[objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self];
并使用这样的映射:
//Creamos un mapeo para el Login mapping
RKObjectMapping *sessionMapping = [RKObjectMapping mappingForClass:[UrSession class]];
[sessionMapping mapKeyPathsToAttributes:@"uid", @"uid", @"token", @"token", nil];
[objectManager.mappingProvider setMapping:sessionMapping forKeyPath:@"result"];
一切正常,来自服务器的响应映射到我的 Object UrSession。
但是现在我们需要更改客户端,因为服务器需要 POST 而不是 GET 来登录。好的,那么这是我的代码:
RKClient *myClient = [RKClient sharedClient];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
//User and password params
[params setObject:password forKey:@"password"];
[params setObject:email forKey:@"email"];
//Parsing rpcData to JSON!
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
NSError *error = nil;
NSString *json = [parser stringFromObject:params error:&error];
//If no error we send the post, voila!
if (!error){
[myClient post:@"/default/api/loginPost.json" params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON] delegate:self];
}
好的,直到这里一切正常。服务器对我的 POST 的响应,但我的代码没有映射此响应!!.. 为什么?我知道我丢失了一些东西,但我不明白为什么使用 GET Restkit 映射来自服务器的 JSON,但使用 GET 却没有。
我有点绝望,因为我找不到适用于我的代码的解决方案。
谢谢您的帮助。
最后这是代码:
NSMutableDictionary *paramsDic = [[NSMutableDictionary alloc] init];
//User and password params
[paramsDic setObject:password forKey:@"password"];
[paramsDic setObject:email forKey:@"email"];
//Parsing to JSON
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
NSError *error = nil;
NSString *json = [parser stringFromObject:paramsDic error:&error];
RKParams *params = [RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding]MIMEType:RKMIMETypeJSON];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"loginPost.json" usingBlock:^(RKObjectLoader * loader)
{
loader.method = RKRequestMethodPOST;
loader.params = params;
loader.delegate = self;
}];