2

我正在使用开发分支来避免从旧的开始,因为新的正在推出。不幸的是,我无法弄清楚如何做基础知识(我是 RestKit 的新手)。

我正在尝试完成的基本步骤是从使用 2 个参数调用“/auth/login/”开始并检索 json 文档。任何帮助将不胜感激!

更新1:我问错问题了吗?我错过了什么?人们只是不将 Restkit 用于项目吗?

更新 2:收到此错误时我应该寻找什么?我有一个类映射和一个路径模式,但我真的不明白我应该做什么。

Code=1001 "Unable to find any mappings for the given content"

我刚刚在https://github.com/RestKit/RestKit/blob/development/README.md找到了这个更新的自述文件

更新 3:我尝试了多种方法来进行简单的呼叫/响应/成功呼叫,但没有。我可以在输出中看到调用成功,但 RestKit 总是抱怨它无法映射内容。我真的不明白。生成的json大多如下:

{
      "email" : "me@here.com",
      "fullname" : "Full Name"
}

无论我尝试什么,我都无法让 RestKit 弄清楚这一点。帮助?任何人?

更新 4:我将有效负载更改为以下内容,另外我更改了描述符语句,但结果没有变化。调用成功,RestKit 因错误 1001 而失败。它仍然显示我的 keyPath=null。我错过了什么?

{
    "whoami" : {
      "email" : "me@here.com",
      "fullname" : "Full Name"
    }
} 

[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:mymap
                                               pathPattern:@"/auth/login/"
                                                   keyPath:@"whoami"
                                               statusCodes:statusCodes]];
4

1 回答 1

1

也许这可以帮助..请求可能如下所示:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:email forKey:@"email"];
[params setObject:fullname forKey:@"fullname"];


NSMutableURLRequest *rq = [manager requestWithObject:[User new] method:RKRequestMethodPOST path:@"http://url" parameters:params];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[User mapping] pathPattern:nil keyPath:nil statusCodes:nil];

RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:rq responseDescriptors:@[responseDescriptor]];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {

     //success

} failure:^(RKObjectRequestOperation *operation, NSError *error) {

     //error

}];
[operation start];

和映射:

+ (RKObjectMapping *)mapping
{
    RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[self class]];
    [requestMapping addAttributeMappingsFromDictionary:@{
     @"ContactDetail":     @"contactDetail",
     @"IdUser":            @"idUser",
     @"Name":              @"name",
     @"Address":           @"address",
     @"UserSettings":      @"userSettings"
     }];

    // if the obj have relationship
    RKRelationshipMapping *rl = [RKRelationshipMapping relationshipMappingFromKeyPath:@"someKey" toKeyPath:@"toKey" withMapping:[Obj mapping]];
    [requestMapping addPropertyMappingsFromArray:@[rl]];

    return requestMapping;
}
于 2012-12-11T10:04:39.703 回答