这是我的路由设置:
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[UserProfile class] pathPattern"/api/v1/users" method:RKRequestMethodPOST]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[UserProfileAndStatistics class] pathPattern"/api/v1/profile/:userId" method:RKRequestMethodGET]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[PrivateUserProfile class] pathPattern"/api/v1/privateprofile/" method:RKRequestMethodGET]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[PrivateUserProfile class] pathPattern"/api/v1/privateprofile/" method:RKRequestMethodPOST]];
这是我注册映射的方式:
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:nil];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
//the serialisation step
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[mapping inverseMapping] objectClass:[mapping objectClass] rootKeyPath:nil];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
最后我打电话:
[[RKObjectManager sharedManager] postObject:userProfile path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) { ... }
正确发布后,我得到一个 HTTP 状态代码 200,并且数据进入服务器。这意味着序列化步骤正在工作。当响应回来时,我得到一个错误:
Adding mapping error: Expected an object mapping for class of type 'rivateUserProfile', provider returned one for 'UserProfileAndStatistics'
现在 PrivateUserProfile
和 UserProfileAndStatistics
的映射非常相似
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[PrivateUserProfile class]];
[mapping addAttributeMappingsFromDictionary{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Email" : @"emailAddress",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar"
}];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[UserProfileAndStatistics class]];
[mapping addAttributeMappingsFromDictionary{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar",
@"osts" : @"posts",
@"Followers" : @"followers",
@"Following" : @"following"
}];
但是为什么 RestKit 会选择其中一个呢?如何成功调试? RKRoute 和 RKObjectMapping 有没有关系。 PrivateUserProfile 的映射被用于序列化步骤,那么为什么不将它用于对应的反序列化,以及如何使用它?
您需要两个响应描述符 - 一个带有路径 /api/v1/privateprofile/
用于类 PrivateUserProfile
,另一个带有路径 /api/v1/
:UserProfileAndStatistics
类的 profile/:userId
[manager addResponseDescriptorsFromArray[
[RKResponseDescriptor responseDescriptorWithMapping:privateUserProfileMapping
pathPattern"/api/v1/privateprofile/"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)],
[RKResponseDescriptor responseDescriptorWithMapping:userProfileAndStatisticsMapping
pathPattern"/api/v1/profile/:userId"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]
]];
关于ios - RestKitOST 反序列化 - 期望 A 类的对象映射,提供者为 B 返回一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205205/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |