使用RESTKit我正在发出一个接收对象的GET请求,但有时服务器会返回一个字面上的响应" null"如果数据库中没有记录,则没有对象。该请求失败,其中包含"已加载的无法处理的响应(200),内容类型为&application; / json'"因为它无法映射" null"响应一个对象,但我希望请求成功,只返回一个nil对象。以下是我发出GET请求的方式:
NSDictionary *objMapping = @{
@"PROPERTY": @"property",
};
RKObjectMapping *myMapping = [RKObjectMapping mappingForClass:[MyClass class]];
[myMapping addAttributeMappingsFromDictionary:objMapping];
RKObjectManager* objectManager = [RKObjectManager sharedManager];
NSString *methodURL = [NSString stringWithFormat:@"%@/MyMethod", basePath];
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:methodURL parameters:@{@"myParam": myParamValue}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:myMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:statusCodes];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
MyClass *myObj = [mappingResult firstObject];
if (success)
{
success(myObj);
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if(failure)
{
failure(error);
}
}];
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:objectRequestOperation];