使用未映射到POST对象的RestKit处理POST响应

时间:2013-08-16 03:42:00

标签: ios objective-c restkit restkit-0.20

我有一种情况,我正在做一个对象的帖子(不是托管的,只是一个NSObject派生的),但REST API的响应不是所发布对象类型的JSON表示。这完全是另一回事。有可能处理这个吗?

假设我发布了一个对象(JSON):

{
  "prop1" : 123,
  "prop2" : 5
}

如果不是具有属性prop1prop2的对象的响应,就像默认情况下RestKit所期望的那样,但是其他东西:

{
  "data" : [1,2,3,4,5,6],
  "message" : "hello world"
}

我见过将operation.targetObject设置为nil的示例,但(我相信)这只是在Core Data托管方案中。

我正在尝试这样简单的事情:

RKObjectRequestOperation *operation = [self.objectManager appropriateObjectRequestOperationWithObject:body method:RKRequestMethodPOST path:@"items" parameters:nil];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
   // success is called, but the result does not contain what the response JSON is
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
  // failure
}];

[operation setWillMapDeserializedResponseBlock:^id(id deserializedResponseBody) {
    // deserializedResponseBody is always nil
}];

[operation start];

我以为我能够查看setWillMapDeserializedResponseBlock中的响应内容,但它总是为零。

由于发布的对象具有prop1prop2属性,以及默认情况下RestKit如何工作,因此它需要具有所述属性的JSON。但是,由于结果与发布的对象无关,prop1prop2只是零(我无论如何都不关心)。

顺便说一句,我已经正确设置了请求映射,因此正在操作对象中传递的body正确映射我的NSObject并将其以正确的JSON格式发送到服务器。

有没有办法可以使用RestKit进行这样的调用,并在成功事件中手动拉出我要查找的内容?即使它是JSON响应的字典表示?

3 个答案:

答案 0 :(得分:16)

我只是为您的回复创建一个新对象,并将您的回复映射到该对象。

物件

@interface MYRequest : NSObject

@property NSNumber *prop1;
@property NSNumber *prop2;

@end

@interface MYResponse : NSObject

@property NSArray *data;
@property NSString *message;

@end

映射

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"prop1", @"prop2"]];

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[MYResponse class]];
[requestMapping addAttributeMappingsFromArray:@[@"data", @"message"]];

描述符

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                               objectClass:[MYRequest class]
                                                                               rootKeyPath:nil
                                                                                    method:RKRequestMethodPOST];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                        method:RKRequestMethodPOST
                                                                                   pathPattern:@"items"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

对象管理器

[self.objectManager addRequestDescriptor:requestDescriptor];
[self.objectManager addResponseDescriptor:responseDescriptor];

执行

MYRequest *body = [MYRequest new];
[body setProp1:@123];
[body setProp2:@5];

[self.objectManager postObject:body
                          path:@"items"
                    parameters:nil
                       success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                           MYResponse *result = [mappingResult firstObject];
                           NSLog(@"Data: %@\nMessage: %@", [result data], [result message]);
                       }
                       failure:^(RKObjectRequestOperation *operation, NSError *error) {
                           NSLog(@"Error: %@", error);
                       }];

答案 1 :(得分:1)

您可以将源对象转换为字典并将其发送。然后响应数据自然会被映射到字典中。这应该是对映射的最小更改以及对其他代码的少量更改(使用KVC dictionaryWithValuesForKeys:)。

答案 2 :(得分:0)

Simply used below code

//If we received error message dictionary in API then we can convert RKObjectRequestOperation Data into Json dictionary 

 if let json = try JSONSerialization.jsonObject(with: operation.httpRequestOperation.responseData, options: .mutableContainers) as? [String: Any] {
//here we can catch our error message..
let message:String = json["ErrorMessage"] as! String?
 print("Error Message: \(message)")
}