2012-11-12 132 views
2

我正在使用开发分支来避免随着新版本的推出而开始。不幸的是,我无法弄清楚如何做基础(我是RestKit的新手)。如何使用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" : "[email protected]", 
     "fullname" : "Full Name" 
} 

不管我怎么努力,我不能让RestKit明白这一点。帮帮我?任何人?

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

{ 
    "whoami" : { 
     "email" : "[email protected]", 
     "fullname" : "Full Name" 
    } 
} 

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

你看在github上站点上的文档? https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md。或者搜索一些例子? http://liebke.github.com/restkit-github-client-example/ – marvin

+2

大多数示例都使用不再存在的方法,甚至有些类不再存在,例如RKClient。我一直在寻找一段时间,试图超越学习曲线。详细的例子似乎显示了一种工作方式,但只有服务器完全按照RestKit希望服务器工作的方式工作。做别的事情似乎很难,主要是因为它不知道该怎么做。是的,我真的很困惑:) – darren

+0

你引用的第二个例子我可以使大部分工作,除了调用“RKObjectMapper * mapper = objectManager.mapper”不再存在。 – darren

回答

1

也许这可以help..the请求可能是这样的:

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; 
}