RESTKit 异常:在搜索的关键路径中未找到可映射的对象表示
Posted
技术标签:
【中文标题】RESTKit 异常:在搜索的关键路径中未找到可映射的对象表示【英文标题】:RESTKit exception: No mappable object representations were found at the key paths searched 【发布时间】:2016-04-25 16:49:39 【问题描述】:我遇到了一些问题。服务器收到请求,但未正确处理映射。我不明白我做错了什么。
日志:
2016-04-25 19:39:16.114 cifrosvit[1063:15363] I restkit:RKLog.m:49 RestKit logging initialized...
2016-04-25 19:39:16.413 cifrosvit[1063:15363] I restkit.network:RKObjectRequestOperation.m:150 GET 'http://cifrosvit.com/api/get_info/?info=banners'
2016-04-25 19:39:17.188 cifrosvit[1063:15505] I restkit.network:RKObjectRequestOperation.m:222 GET 'http://cifrosvit.com/api/get_info/?info=banners' (200 OK / 7 objects) [request=0.7722s mapping=0.0021s total=0.8101s]
2016-04-25 19:41:17.508 cifrosvit[1063:15363] I restkit.network:RKObjectRequestOperation.m:150 GET 'http://cifrosvit.com/api/user.sign_up/?adr=terwtrwe&city=fwferwtw&email=yrurweqrqw%40rfewq.rewq&name=fsdfsdfdsa&password=12345&phone_mob=54323425342'
2016-04-25 19:41:18.040 cifrosvit[1063:16373] D restkit.object_mapping:RKMapperOperation.m:407 Executing mapping operation for representation:
data = (
);
errors = (
);
success = true;
and targetObject: (null)
2016-04-25 19:41:18.040 cifrosvit[1063:16373] D restkit.object_mapping:RKMapperOperation.m:433 Finished performing object mapping. Results: (null)
2016-04-25 19:41:18.041 cifrosvit[1063:16374] E restkit.network:RKObjectRequestOperation.m:215 GET 'http://cifrosvit.com/api/user.sign_up/?adr=terwtrwe&city=fwferwtw&email=yrurweqrqw%40rfewq.rewq&name=fsdfsdfdsa&password=12345&phone_mob=54323425342' (200 OK / 0 objects) [request=0.5320s mapping=0.0000s total=0.5345s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://cifrosvit.com/api/user.sign_up/?adr=terwtrwe&city=fwferwtw&email=yrurweqrqw%40rfewq.rewq&name=fsdfsdfdsa&password=12345&phone_mob=54323425342', which failed to match all (0) response descriptors:, NSErrorFailingURLStringKey=http://cifrosvit.com/api/user.sign_up/?adr=terwtrwe&city=fwferwtw&email=yrurweqrqw%40rfewq.rewq&name=fsdfsdfdsa&password=12345&phone_mob=54323425342, NSErrorFailingURLKey=http://cifrosvit.com/api/user.sign_up/?adr=terwtrwe&city=fwferwtw&email=yrurweqrqw%40rfewq.rewq&name=fsdfsdfdsa&password=12345&phone_mob=54323425342, NSUnderlyingError=0x7fa58a300000 Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched:
The representation inputted to the mapper was found to contain nested object representations at the following key paths: data, errors, success
This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null, DetailedErrors=(
), keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.
代码:
RKObjectMapping* objectMapping = [RKObjectMapping mappingForClass:[ResponseModel class]];
[objectMapping addAttributeMappingsFromArray:@[@"success", @"errors"]];
[objectMapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"data"
toKeyPath:@"data"
withMapping:[RKObjectMapping mappingForClass:[NSArray class]]]
];
RKResponseDescriptor* responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodPOST
pathPattern:@"user.sign_up/"
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://cifrosvit.com/api/"]];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
[objectManager addResponseDescriptor:responseDescriptor];
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
[objectManager getObjectsAtPath:responseDescriptor.pathPattern parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSLog(@"success");
failure:^(RKObjectRequestOperation *operation, NSError *error)
NSLog(@"failure");
];
ResponseModel 接口:
@interface ResponseModel : NSObject
@property NSString* success;
@property id data;
@property (strong, nonatomic) NSArray* errors;
@end
请求:
"success": "true",
"data": [],
"errors": []
为什么 RESTKIT 总是调用失败块?
【问题讨论】:
【参考方案1】:您可能只需要更改这一行,因为它看起来像是在设置一个 nil 响应描述符?
[objectManager addResponseDescriptor:self.responseDescriptor];
所以删除self.
,因为您似乎没有设置实例变量,您只有一个局部变量。
您也在使用method:RKRequestMethodPOST
,但使用getObjectsAtPath
发出请求,因此RestKit 无法将GET
与POST
匹配。
【讨论】:
我发现这是个问题。我不明白为什么,但是当我将请求方法从 RKRequestMethodPOST 更改为 RKRequestMethodAny 时,它开始工作。这个请求的实际类型是 POST。 好的,好点,但这不是 POST,而是 GET,因为您使用的是getObjectsAtPath
您的回答完全正确。我错过了文档中的这个项目。谢谢。以上是关于RESTKit 异常:在搜索的关键路径中未找到可映射的对象表示的主要内容,如果未能解决你的问题,请参考以下文章
PhoneGap / Cordova 回调错误 - 在 PhoneGap 中未执行 RestKit 完成功能
如何将展平的对象序列化回服务器,在 RestKit 中未展平