在没有嵌套KVC的情况下映射来自POST的响应

时间:2014-06-16 08:13:34

标签: json restkit-0.20

我正在尝试将属性映射到RESTKit中的一个类而没有嵌套的KVC,并且没有太多运气,即使响应包含值,我也会继续为我的值获取nil。

我已经阅读了对象映射手册并尝试了没有路径模式的路径模式等。有没有人有任何见解?

来自服务器的请求和响应

2014-06-16 15:58:07.162 game[5961:60b] T 

restkit.network:RKObjectRequestOperation.m:178 POST 'https://api.myURL.com/api/0_0_1/notify/player':
request.headers=
{
    Accept = "application/json";
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
    "Content-Type" = "application/json; charset=utf-8";
    "User-Agent" = "game/1470 (iPhone; iOS 7.1; Scale/2.00)";
}
request.body=
{
    "game":"539e75acadae8fb03900057e",
    "to":"533bd7eb5317b88f61000006",
    "message":"‎Ben Fowler is waiting for you to play the game",
    "from":"5332b4f5edfc9beb7900004d"
}
2014-06-16 15:58:08.856 game[5961:5607] T restkit.network:RKObjectRequestOperation.m:248 POST 'https://api.myURL.com/api/0_0_1/notify/player' (200 OK / 1 objects) [request=1.6911s mapping=0.0020s total=1.6989s]:
        response.headers={
            Connection = "keep-alive";
            "Content-Length" = 42;
            "Content-Type" = "application/json; charset=utf-8";
            Date = "Mon, 16 Jun 2014 07:58:09 GMT";
            "X-Powered-By" = Express;
        }
        response.body=
        {
          "result": 1,
          "notify": "871720292"
        }

正在映射的类

-(void)WtfGameNotifcationResponse
{
RKObjectMapping *notifyMapping = [RKObjectMapping mappingForClass:[WTFGameNotification class]];

[notifyMapping addAttributeMappingsFromDictionary:
 @{
 @"result" : @"result",
 @"notify" : @"notifyID",
 }];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:notifyMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:@"notify/player"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

}

有问题的课程

@interface WTFGameNotification : NSObject

@property (nonatomic,copy) NSString* to;
@property (nonatomic,copy) NSString* from;
@property (nonatomic,copy) NSString* gameID;
@property (nonatomic,copy) NSString* message;

@property (nonatomic,strong) NSNumber* result;
@property (nonatomic,copy) NSString *notifyID;

+ (WTFGameNotification*)Create;

@end

2 个答案:

答案 0 :(得分:0)

似乎如果你试图从POST映射响应,这是应该使用的方法和语法。

+ ( void )SendNotification:(GameInfo*)gameInfo withMessage:(NSString*)message forView:(UIViewController*)view
{
    WTFGameNotification *_wtfGameNotif = [WTFGameNotification Create];

    [_wtfGameNotif setTo:[gameInfo opponentWTFID]];
    [_wtfGameNotif setFrom:[PlayerWTFInfo Load]._wtfID];
    [_wtfGameNotif setGameID:[gameInfo gameID]];
    [_wtfGameNotif setMessage:message];

    if ([_wtfGameNotif to] && [_wtfGameNotif gameID])
    {
        RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] appropriateObjectRequestOperationWithObject:_wtfGameNotif
                                                                                                                    method:RKRequestMethodPOST
                                                                                                                      path:nil
                                                                                                                parameters:nil];
        RFResponse *r1 = [RFResponse new];
        operation.targetObject = r1;

        [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
        {
            if (r1.result.integerValue != 0)
            {
                [Utilities FacebookPostWithResult:@{@"fbId": r1.notify}];
            }
        }
        failure:^(RKObjectRequestOperation *operation, NSError *error)
        {
            DDLogError(@"POST REQUEST FAILED IN CONFIRMFOTOVC : %@",[error description]);

             if (view)
             {
                 [LoadingView PopToRootShutterCloseOnView:view willOpen:YES];
             }
        }];

        [operation start];

    }
    else
    {
        DDLogInfo(@"NO PLAYER ID");
    }
}

答案 1 :(得分:0)

如果要将对象发布到服务器,那么您拥有的响应描述符将不匹配,因为您将其链接到带有method:RKRequestMethodGET的GET。您需要将其更改为method:RKRequestMethodPOST

使用@"notify/player"的路径模式时,基本网址应设置为https://api.myURL.com/api/0_0_1/,但看起来这不是一个问题。