我在做一个对象的帖子(不是托管的,只有NSObject派生的),但是REST的响应不是被发布的那种对象的JSON表示。这完全是另一回事。能处理这件事吗?
假设我发布了一个对象(JSON):
{
"prop1" : 123,
"prop2" : 5
}
以及响应(如果不是属性prop1
和prop2
的对象,默认情况下RestKit所期望的那样),但是其他内容:
{
"data" : [1,2,3,4,5,6],
"message" : "hello world"
}
我见过operation.targetObject被设置为零的例子,但是(我相信)这只是在核心数据管理的场景中。
我试着做这样简单的事情:
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中的响应内容,但始终是零。
由于posted对象具有prop1
和prop2
属性,以及默认情况下RestKit的工作方式,所以它期望JSON具有上述属性。但是,由于结果与posted对象无关,所以prop1
和prop2
只是零(反正我也不在乎)。
顺便说一下,我已经正确设置了请求映射,因此正在传递操作对象的body
正确地映射了我的NSObject并以正确的JSON格式发送给服务器。
有什么方法可以让我用RestKit打这样的电话,然后手动提取我在成功事件上要找的东西吗?即使它是JSON响应的字典表示?
发布于 2013-08-16 16:18:52
我只需要为您的响应创建一个新的对象,并将您的响应映射到这个对象。
对象
@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);
}];
发布于 2013-08-16 09:43:24
您可以将源对象转换为字典,然后将其发送。然后,响应数据将自然映射到字典中。这应该是对映射的最小更改,也应该是对其他代码(使用KVC dictionaryWithValuesForKeys:
)的小改动。
发布于 2017-03-24 07:31:20
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)")
}
https://stackoverflow.com/questions/18265461
复制相似问题