我正在使用Restkit和对象映射,但我似乎收到错误Encountered errors during mapping: Expected an object mapping for class of type 'Rating', provider returned one for 'Error'
- 现在我已经为Rating和Error对象设置了映射和序列化,所以我不明白为什么它困惑。有人可以帮忙吗?
这是从类
返回的映射// Rating class
+ (RKObjectMapping *)getRestKitObjectMapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Rating class]];
[mapping mapAttributes:@"id", @"mics", @"mdate", @"ipaddress", nil];
[mapping mapKeyPath:@"user_id" toAttribute:@"userID"];
[mapping mapKeyPath:@"fid" toAttribute:@"fid"];
return mapping;
}
// Error class
+ (RKObjectMapping *)getRestKitObjectMapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Error class]];
[mapping mapKeyPath:@"code" toAttribute:@"code"];
[mapping mapKeyPath:@"message" toAttribute:@"message"];
return mapping;
}
这是对象路由
[[RKObjectManager sharedManager].router routeClass:[Rating class] toResourcePath:@"/mic/:id"];
[[RKObjectManager sharedManager].router routeClass:[Rating class] toResourcePath:@"/mic" forMethod:RKRequestMethodPOST];
[[RKObjectManager sharedManager].mappingProvider setMapping:[Rating getRestKitObjectMapping] forKeyPath:@"ratings"];
[[RKObjectManager sharedManager].mappingProvider setMapping:[Error getRestKitObjectMapping] forKeyPath:@"error"];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[[Rating getRestKitObjectMapping] inverseMapping] forClass:[Rating class]];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[[Error getRestKitObjectMapping] inverseMapping] forClass:[Error class]];
Rating *rating = [[Rating alloc] init];
[[RKObjectManager sharedManager] postObject:rating delegate:self];
这些是委托方法,didLoadObjects
确实被称为
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
NSLog(@"Object loading failed with error: %@ ----- body: %@", error.localizedDescription, objectLoader.response.bodyAsString);
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"Objects received: %@", objects);
NSLog(@"Respond body: %@", objectLoader.response.bodyAsString);
}
这是JSON响应
{"error":{"error":1,"code":342,"message":"You have already rated this one"}}
当我查看REST PHP后端的$ _POST值时,它实际上是空的,这意味着它没有收到发布对象......为什么会这样?我的路由似乎没问题?
答案 0 :(得分:0)
您是否可以控制您的后端服务?
如果是这样,我会改变你发回数据的方式。当您进行“postObject”调用时,唯一预期的响应是“评级”。我意识到你设置了一个“错误”映射,但是因为你发布的对象是一个“评级”对象,所以它期待回来。
这里更合适的是服务将HTTP状态代码更改为403(禁止),或者不是200(OK)。然后你可以处理“objectLoader:didFailWithError:”委托方法中的错误。