无法在 POST 之后映射接收到的对象

Can't map received object after a POST

向服务器发出 POST 请求后,返回的对象是不可映射的,尽管我已经为它创建了所有必要的基础结构。

为了简化我的问题,我创建了一个 "POST object" class,另一个用于响应对象; (以下是我的代码的重要部分)

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 object"class实现

@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) {
                                    }];

现在 class 的 "RECEIVED Object"

@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];

错误

我将它整齐地隔开,以使其更具可读性。

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://mywebsite.com/app_dev.php/api/v2/user/login', which failed to match all (0) response descriptors:,

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 "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: authorized, email, expiry, firstname, lastname, mobile

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.}

请帮忙:(

您有 2 个问题:

  1. 您发出的请求的路径模式与响应描述符不匹配
  2. 当 post 访问一个特定的对象时,响应被映射到同一个对象,所以你应该用一个对象来表示整个事物,或者 post 来自字典

解决方法很简单。 POST 请求的 keyPath 错误。只好修改成这个了;

[[RKObjectManager sharedManager] postObject:loginUser
                                       path:@"user/login"
                                 parameters:nil
                                    success:^(RKObjectRequestOperation  *operation, RKMappingResult *mappingResult) {
                                    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                    }];