Ios 带休息套件的邮政
我有一个JSON结束路径,它接受以下格式的post请求Ios 带休息套件的邮政,ios,objective-c,restkit,restkit-0.20,Ios,Objective C,Restkit,Restkit 0.20,我有一个JSON结束路径,它接受以下格式的post请求 {'values': [ { "date":<measurement date as Unix time stamp> "value":<weight> } { "date":<measurement date as Unix time stamp> "value":<weight> } ...]} 数据
{'values': [
{
"date":<measurement date as Unix time stamp>
"value":<weight>
}
{
"date":<measurement date as Unix time stamp>
"value":<weight>
}
...]}
数据结构的编辑 我的数据结构很简单(我想):
我想发布一个包含条目的EntryCollection。EntryCollection的映射是“entries->values”,Entry的映射是“date->date,weight->value”。在任何情况下,您的JSON请求负载必须确认为以下数据结构:
NSArray
|
|______NSDictionary ->Key: Date Value: weight
| ->Key: value Value: weight
|
|______NSDictionary ->Key: Date Value: weight
| ->Key: value Value: weight
|
|______NSDictionary ->Key: Date Value: weight
->Key: value Value: weight
NSArray
和NSDictionary
都与JSON
数据格式完全兼容。我不知道您的底层对象结构,但最终这个数组应该作为请求负载NSData
发布,您就可以完成了。好吧,如果您在映射方面有问题,那么您必须显示您的模型、类和映射,或者将RKLogConfigureByName(“RestKit/ObjectMapping”,RKLogLevelTrace)放入其中代码>在代码中的某个地方,让我们看看输出
另一种选择是,如果您的实体结构与要发布到服务器的内容不同,则可以使用嵌入式AFNetworking
client并执行简单的请求
[[RKObjectManager sharedManager].HTTPClient postPath:@"" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"WHOOO");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
}];
我找到了解决办法。事实证明,我需要一个关系映射来描述JSON中隐含的层次结构。因为每个值实体都没有类型,所以我创建了一个“空”映射,并将关系映射添加到其中。
我还忘了设置正确的MIMEType和反转类的属性映射。我想需要在restkit中呆上几天才能掌握它
RKObjectMapping *entryMapping = [RKObjectMapping requestMapping];
[entryMapping addAttributeMappingsFromDictionary:[SingleEntry attributesMapping]];
RKObjectMapping *entrySerializedMapping = [entryMapping inverseMapping];
RKRelationshipMapping *entryRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"entries" toKeyPath:@"values" withMapping:entrySerializedMapping];
RKObjectMapping *valueMapping = [RKObjectMapping requestMapping];
[valueMapping addPropertyMapping:valueMapping];
RKRequestDescriptor *descriptor = [RKRequestDescriptor requestDescriptorWithMapping:valueMapping objectClass:[EntriesCollection class] rootKeyPath:nil];
[self.objectManager addRequestDescriptor:descriptor];
self.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
NSString *path = [self pathForPOST];
[self.objectManager postObject:entryCollection path:path parameters:nil success:nil failure:nil];
JSON可以有一个字典作为根,这对映射问题没有任何帮助……您的类结构确实不清楚,因为您的映射中有许多不同的类,并且大多数映射都是局部的。您可能会发现创建映射更容易,就好像您正在解析JSON,然后使用相反的方法。否则您需要显示完整的类结构。我已经用数据结构更新了我的问题。
[[RKObjectManager sharedManager].HTTPClient postPath:@"" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"WHOOO");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
}];
RKObjectMapping *entryMapping = [RKObjectMapping requestMapping];
[entryMapping addAttributeMappingsFromDictionary:[SingleEntry attributesMapping]];
RKObjectMapping *entrySerializedMapping = [entryMapping inverseMapping];
RKRelationshipMapping *entryRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"entries" toKeyPath:@"values" withMapping:entrySerializedMapping];
RKObjectMapping *valueMapping = [RKObjectMapping requestMapping];
[valueMapping addPropertyMapping:valueMapping];
RKRequestDescriptor *descriptor = [RKRequestDescriptor requestDescriptorWithMapping:valueMapping objectClass:[EntriesCollection class] rootKeyPath:nil];
[self.objectManager addRequestDescriptor:descriptor];
self.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
NSString *path = [self pathForPOST];
[self.objectManager postObject:entryCollection path:path parameters:nil success:nil failure:nil];