如何使用Restkit 0.10.X来POST JSON和映射响应

时间:2013-01-10 12:06:55

标签: ios post restkit

当我做出像这样的

//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;
  }];

1 个答案:

答案 0 :(得分:0)

如果使用RKClient发出请求,则响应将不会映射到对象。您必须使用RKObjectManager进行映射。

您可以使用以下块来使用方法loadObjectAtResourcePath发出POST请求:

 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/path" usingBlock:^(RKObjectLoader * loader)
  {
                loader.method = RKRequestMethodPost;
                loader.params = [RKRequestSerialization serializationWithData:[requestString dataUsingEncoding:NSUTF8StringEncoding MIMEType:RKMIMETypeJSON];
                loader.delegate = self;
  }];

响应将映射到您的对象。