gpt4 book ai didi

routing - RestKit 0.20.2 路由和映射

转载 作者:行者123 更新时间:2023-12-02 02:01:33 25 4
gpt4 key购买 nike

我有以下 JSON 和它的路由映射。单项 JSON

{
"quantity" : 0,
"id" : 1,
"version" : 0,
"sku" : "sku1",
"title" : "title1",
}

所有项目JSON如下:-

[
{
"quantity" : 2,
"id" : 1,
"version" : 0,
"sku" : "sku1",
"title" : "title1",
},
{
"quantity" : 4,
"id" : 2,
"version" : 0,
"sku" : "sku2",
"title" : "title2",
}
]

这是映射:-

RKEntityMapping *newItemMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Item class]) inManagedObjectStore:manager.managedObjectStore];
newItemMapping.identificationAttributes = @[@"id"];
[newItemMapping addAttributeMappingsFromDictionary:@{
@"id" : @"id",
@"version" : @"version",
@"title" : @"title",
@"sku" : @"sku"
}];

这是路由信息。

[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items/:id" method:RKRequestMethodGET]];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items" method:RKRequestMethodPOST]];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items/:id" method:RKRequestMethodPUT]];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items/:id" method:RKRequestMethodDELETE]];

GET 请求工作正常并使用上述映射正确加载对象。

[[RKObjectManager sharedManager] getObject:sampleItemObject path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Items: %@", mappingResult);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];

但是 POST 请求失败并显示如下消息:-

[[RKObjectManager sharedManager] postObject:newItem path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failure saving post: %@", error.localizedDescription);
}];

错误信息:-

保存帖子失败:预期的内容类型{( “应用程序/x-www-form-urlencoded”, “应用程序/json”)}, 得到文本/html

请协助解决问题?

谢谢,佛基

最佳答案

在配置共享管理器时或在发出发布请求之前的某个时候添加此行:

[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

此外,请确保为您的对象定义了序列化映射。这些将允许 RestKit 将对象作为格式正确的 JSON 字符串发送到服务器。

关于routing - RestKit 0.20.2 路由和映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17020830/

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