gpt4 book ai didi

ios - POST 后无法映射接收到的对象

转载 作者:行者123 更新时间:2023-11-29 01:13:10 25 4
gpt4 key购买 nike

在向服务器发出 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];

错误

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

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. 发布特定对象时,响应会映射到同一个对象,因此您应该使用一个对象来处理整个事件或从字典中发布

关于ios - POST 后无法映射接收到的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com