我是 Web 服务的新手,并且 GET 工作正常。当我尝试发布时,我正在发送这个 JSON 字符串:
{"frequency":"None","leader":"test@test.com"}
但是服务器期望这样:
{
"meta":
{"limit": 20, "total_count": 2},
"objects":
[{"frequency": "None", "leader": "jsmith@gmail.com"},
{"frequency": "None", "leader": "jsmith@gmail.com"}]
}
在我的 NewMeetingRK 对象中,我试图映射到“对象”,如下所示:
mgr.serializationMIMEType = RKMIMETypeJSON;
RKObjectMapping* newmtg = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[newmtg mapKeyPath: @"frequency" toAttribute:@"repeat" ];
[newmtg mapKeyPath: @"leader" toAttribute:@"leader" ];
RKObjectMapping* newmtgSerializeMapping = [newmtg inverseMapping];
[mgr.mappingProvider setSerializationMapping:newmtgSerializeMapping forClass:[NewMeetingRK class]];
[mgr.mappingProvider setMapping:newmtgSerializeMapping forKeyPath:@"objects"];
[mgr.router routeClass:[NewMeetingRK class] toResourcePath:@"" forMethod:RKRequestMethodPOST];
这是行不通的。我尝试将 toResourcePath 更改为 @"/objects" 但这也不起作用。知道如何将值发送到服务器上的第二组 JSON 值吗?
- -编辑 - -
正如@HotLicks 指出的那样,我错过了数组 - 谢谢。但我仍然无法让我的带有 JSON 值的简单帖子正常工作。
我可以用 RKClient 做一些简单的事情来代替 RKObjectManager 吗?
client = [RKClient clientWithBaseURL:[RKURL URLWithBaseURLString:@"https://appzute.appspot.com"]];
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
[client setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableArray *meetingArray = [NSMutableArray arrayWithCapacity:100];
[meetingArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
self.leaderEmail.text, @"leader",
self.repeatLabel.text, @"frequency",
nil]];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:meetingArray, @"objects", nil];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSLog (@"jsonString is: %@",jsonString);
[client post:@"/api/meeting/?format=json&username=testuser@test.com&api_key=sdf7asd87fs8df78sdf" params:jsonString delegate:self];
jsonString 的值看起来很完美: {"objects":[{"leader":"me@me.com","frequency":"None"}]} 问题是 params:jsonString 无效,因为它需要对象不是字符串。但是如果我使用 params: jsonDictionary 然后我得到这个日志文件: Sent RKRequest: objects[][leader]=me%40me.com&objects[][frequency]=None I restkit.network:RKRequest.m:676 Status Code: 500
我可能做错了什么?
谢谢!