POST与休息套件

时间:2013-07-05 16:18:12

标签: ios objective-c restkit restkit-0.20

我有一个JSON结束路径,它接受以下格式的帖子请求。

  {'values': [
    {
     "date":<measurement date as Unix time stamp>
     "value":<weight>
    }
    {
     "date":<measurement date as Unix time stamp>
     "value":<weight>
    }
     ...]}

“Values”由“EntryCollection”类表示,而每个值由“Entry”类表示。我很困惑找到将对象映射到JSON表示的正确方法。现在我有以下代码导致错误:“映射操作无法在搜索的关键路径中找到任何嵌套对象表示”。

RKObjectMapping *entryMapping = [RKObjectMapping requestMapping];

RKObjectMapping *valuesMapping = [RKObjectMapping mappingForClass:[EntriesCollection class]];
[valuesMapping addAttributeMappingsFromDictionary:[EntryCollection attributesMapping]];

[singleEntryMapping addAttributeMappingsFromDictionary:[SingleEntry attributesMapping]];
[singleEntryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"entries" toKeyPath:@"entries" withMapping:valuesMapping]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:entryMapping
                                                                               objectClass:mappedClass
                                                                               rootKeyPath:nil];

[self.objectManager addRequestDescriptor:requestDescriptor];
NSString *path = [self pathForPOST];

[self.objectManager postObject:weights path:path parameters:nil success:nil failure:nil];

编辑数据结构

我的数据结构很简单(我想):

EntryCollection  
- NSArray *entries (a collection of objects of type Entry)

Entry  
- NSDate *date  
- NSNumber *weight;

我想POST一个填充条目的EntryCollection。 EntryCollection的映射是“entries - &gt; values”,Entry之一是“date - &gt; date,weight - &gt; value”。

3 个答案:

答案 0 :(得分:1)

在任何情况下,您的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

NSArrayNSDictionary都与JSON数据格式完全兼容。我不知道您的底层对象结构,但最终这个数组应该作为请求有效负载NSData发布,您将完成。

答案 1 :(得分:1)

好吧,如果你在映射方面遇到问题,那么你要么必须展示你的模型,类和映射,要么将RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);放在代码中的某个地方让我们看看输出。

作为替代方案,如果您的实体结构与您希望发布到服务器的内容不同,则可以使用嵌入式AFNetworking客户端并执行简单请求。

 [[RKObjectManager sharedManager].HTTPClient postPath:@"" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"WHOOO");

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //
    }];

答案 2 :(得分:0)

我找到了解决方案。事实证明,我需要一个关系映射来描述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];