gpt4 book ai didi

ios - RestKit 0.20 JSON 对象在 POST 主体中被序列化为 GET 样式请求

转载 作者:可可西里 更新时间:2023-11-01 03:06:58 27 4
gpt4 key购买 nike

我刚开始使用 RestKit 0.20.0,但在创建格式良好的 JSON 请求时遇到了问题。

我得到这个(来自休息工具包日志):

request.body=title=A%20glorious%20walk%20in%20the%20woods&startDateTime=2013-01-13%2016%3A09%3A33%20%2B0000&endDateTime=2013-01-13%2016%3A09%3A43%20%2B0000&points[][longitude]=-122.0307725&points[][latitude]=37.3310798&points[][longitude]=-122.0307334&points[][latitude]=37.33154242&points[][longitude]=-122.03075743&points[][latitude]=37.33138305&points[][longitude]=-122.03075659&points[][latitude]=37.33131185&points[][longitude]=-122.03057969&points[][latitude]=37.33156519&points[][longitude]=-122.03075535&points[][latitude]=37.33144466&points[][longitude]=-122.03076342&points[][latitude]=37.33123666&points[][longitude]=-122.03074488&points[][latitude]=37.33149482&points[][longitude]=-122.03068145&points[][latitude]=37.33155419&points[][longitude]=-122.03062909&points[][latitude]=37.33156564&points[][longitude]=-122.03076853&points[][latitude]=37.33115792

当我想要这个时(带有大括号的普通 json 对象和 points 属性的数组):

{
title: "Something",
startDateTime: "dateinfo",
endDateTime: "moredateinfo",
points: [
{
latitude: "37.33131313",
longitude: "122.4325454"
},
{
latitude: "37.33131313",
longitude: "122.4325454"
}
]
}

我有两个主要对象:一个包含 DLPoint 对象的 NSSet 的 DLWalk(它们是 CoreData 对象,但目前我忽略了它,只专注于创建 HTTP 请求)

这是我用来创建请求的代码:

// Point mapping
RKObjectMapping *mappingPoint = [RKObjectMapping requestMapping];
[mappingPoint addAttributeMappingsFromArray:@[@"latitude", @"longitude"]];
RKRequestDescriptor *reqDescPoint = [RKRequestDescriptor requestDescriptorWithMapping:mappingPoint objectClass:[DLPoint class] rootKeyPath:nil];

// Walk mapping
RKObjectMapping *mappingWalk = [RKObjectMapping requestMapping];
[mappingWalk addAttributeMappingsFromArray:@[@"endDateTime", @"startDateTime", @"title"]];
RKRequestDescriptor *reqDescWalk = [RKRequestDescriptor requestDescriptorWithMapping:mappingWalk objectClass:[DLWalk class] rootKeyPath:nil];


// Define the relationship mapping
[mappingWalk addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"points" toKeyPath:@"points" withMapping:mappingPoint]];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://192.168.1.10:8080"]];
[manager addRequestDescriptor:reqDescWalk];
[manager addRequestDescriptor:reqDescPoint];
[manager addResponseDescriptor:responseDescriptor];

// POST to create
[manager postObject:walk path:@"/walk/save" parameters:nil success:nil failure:nil];

所以问题是:为什么我的 POST 正文中没有得到一个看起来正常的 JSON 对象?

最佳答案

你得到的 request.body 是 URL 编码的,这是 RESTKit 的默认行为,通常工作正常。

如果您希望它是 JSON 编码的,只需在发布查询之前插入这一行

manager.requestSerializationMIMEType=RKMIMETypeJSON;

有关这方面的更多信息,请查看 RKObjectManager 类的 API 文档: requestSerializationMIMEType

关于ios - RestKit 0.20 JSON 对象在 POST 主体中被序列化为 GET 样式请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14377048/

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