在向服务器发出 POST 请求后,返回的对象是不可映射的,尽管我已经为它创建了所有必要的基础设施。
为了简化我的问题,我创建了一个“POST 对象”类,另一个用于响应对象;(以下是我的代码的重要部分)
JSON
要求
{
"USERNAME" : 280S808D08s,
"PASSWORD" : "lkjl123kjl",
"DEVICE" : 12345
}
回复
{
"firstname": "first name here",
"lastname": "last name here",
"mobile": "012938012839",
"email": "john@smith.com",
"expiry": "2016-05",
"authorized": true
}
这是我的“POST 对象”类实现
@interface Login : NSObject
@property NSString *USERNAME;
@property NSString *PASSWORD;
@property NSString *DEVICE;
@end
这些是 API 方法;
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Login class]];
[mapping addAttributeMappingsFromArray:@[@"USERNAME", @"PASSWORD",@"DEVICE"]];
RKRequestDescriptor *loginRequest = [RKRequestDescriptor
requestDescriptorWithMapping:[mapping inverseMapping]
objectClass:[Login class]
rootKeyPath:nil
method:RKRequestMethodPOST];
实际请求
[objectManager addRequestDescriptor:loginRequest];
// POST
Login* loginUser = [Login new];
loginUser.USERNAME = @"96150001505233";
loginUser.PASSWORD = @"gb168362";
loginUser.DEVICE = @"12345";
[[RKObjectManager sharedManager] postObject:loginUser
path:@"/app_dev.php/api/v2/user/login"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
现在是“接收对象”的类
@interface User : NSObject
@property NSString *firstname;
@property NSString *lastname;
@property NSString *email;
@property NSString *mobile;
@end
API方法:
RKObjectMapping *userMap = [RKObjectMapping mappingForClass:[User class]];
[userMap addAttributeMappingsFromArray:@[ @"firstname", @"lastname", @"email", @"mobile"]];
RKResponseDescriptor *userResponse = [RKResponseDescriptor
responseDescriptorWithMapping:userMap
method:RKRequestMethodPOST
pathPattern:@"user/login"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor: userResponse];
错误
我已经将它整齐地隔开以使其更具可读性。
错误域=org.restkit.RestKit.ErrorDomain Code=1001 “没有响应描述符与加载的响应匹配。”
UserInfo={NSLocalizedFailureReason=从 URL ' http://mywebsite.com/app_dev.php/api/v2/user/login ' 加载了 200 响应,它未能匹配所有 (0) 响应描述符:,
NSErrorFailingURLStringKey= http://mywebsite.com/app_dev.php/api/v2/user/login ,
NSErrorFailingURLKey= http://mywebsite.com/app_dev.php/api/v2/user/login ,
NSUnderlyingError=0x7fee81d59440 {Error Domain=org.restkit.RestKit.ErrorDomain Code=1001“在搜索的关键路径中找不到可映射的对象表示。”
UserInfo={NSLocalizedDescription=在搜索的关键路径中找不到可映射的对象表示。,
NSLocalizedFailureReason=映射操作在搜索的关键路径中找不到任何嵌套对象表示:发现输入映射器的表示包含以下关键路径的嵌套对象表示:授权、电子邮件、到期、名字、姓氏、移动
这可能表明您为映射配置了错误的键路径。keyPath=null,DetailedErrors=()}},keyPath=null,NSLocalizedDescription=没有响应描述符与加载的响应匹配。}
请帮忙 :(