在对服务器发出POST请求后,返回的对象是不可映射的,尽管我已经为它创建了所有必要的基础结构。
为了简化我的问题,我创建了一个" POST对象" class,另一个用于响应对象; (以下是我的代码的重要部分)
请求
{
"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
}
@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];
我将它整齐地隔开以使其更具可读性。
错误Domain = org.restkit.RestKit.ErrorDomain Code = 1001"没有回复 描述符匹配加载的响应。"
UserInfo = {NSLocalizedFailureReason =从中加载了200响应 网址' http://mywebsite.com/app_dev.php/api/v2/user/login',失败了 匹配所有(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 {错误 Domain = org.restkit.RestKit.ErrorDomain Code = 1001" No mappable object 在搜索的关键路径中找到了表示。"
UserInfo = {NSLocalizedDescription =无可映射的对象表示 在搜索的关键路径中找到了。
NSLocalizedFailureReason =映射操作无法找到任何内容 搜索的关键路径上的嵌套对象表示: 输入到映射器的表示被发现包含嵌套 以下关键路径中的对象表示:授权,电子邮件, 到期,名字,姓氏,手机
这可能表示您错误配置了关键路径 你的映射。,keyPath = null,DetailedErrors =()}},keyPath = null, NSLocalizedDescription =没有响应描述符与响应匹配 加载。}
请帮助:(
答案 0 :(得分:1)
你有两个问题:
答案 1 :(得分:0)
解决方案相当简单。我的keyPath错误的POST请求。只需将其修改为此;
[[RKObjectManager sharedManager] postObject:loginUser
path:@"user/login"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];