RestKit:如何通过post key在一些密钥中发送对象?

时间:2014-03-04 17:18:08

标签: ios objective-c restkit restkit-0.20

我希望通过post协议将对象发送到服务器。我用它来RestKit框架。

所以,例如:

PData *item = [[PData alloc] init];
item.uri = @"http://google.com";
item.status = @"0";
item.updated = [NSDate date];
item.created = [NSDate date];
item.user_id = @"3";

RKObjectManager *sharedManager = RKObjectManager sharedManager;
[sharedManager postObject:item
                     path:nil
               parameters:nil
                  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    [self printPData:mappingResult];

} failure:^(RKObjectRequestOperation *operation, NSError *error) {

    NSLog(@"\n\n\n == \nERROE = %@",error);

}];

如何在某些键(“数据”)中推送json(包含项目数据)?

(服务器上的这些数据我可以从$ _POST [“data”]中获取)

我该怎么办这样的请求?

{
    "data": {
        "created": "2014-03-05 14:49:30",
        "status": "0",
        "updated": "2014-03-05 14:49:30",
        "uri": "http:\/\/google.com",
        "user_id": "3"
    }
}

现在,我只有这个:

{
    "status": "0",
    "updated": "2014-03-05 14:52:17",
    "user_id": "3",
    "uri": "http:\/\/google.com",
    "created": "2014-03-05 14:52:17"
}

1 个答案:

答案 0 :(得分:0)

听起来您没有在请求描述符中指定密钥路径。目前,代码中的某个地方,您将拥有以下内容:

RKRequestDescriptor *descriptor =
    [RKRequestDescriptor requestDescriptorWithMapping:mapping
                                          objectClass:[PData class]
                                          rootKeyPath:nil
                                               method:RKRequestMethodAny];

您需要将其更改为:

RKRequestDescriptor *descriptor =
    [RKRequestDescriptor requestDescriptorWithMapping:mapping
                                          objectClass:[PData class]
                                          rootKeyPath:@"data"
                                               method:RKRequestMethodAny];

这样RestKit就会知道PData个对象应该映射到data密钥路径。