RESTKit POST请求教程 [英] RESTKit POST Request Tutorial
问题描述
我想知道是否有关于如何使用RESTKit进行POST请求的特定教程.我看过一些教程,但没有发现任何这样的内容:这正是您使用RESTKit进行POST请求的方式."非常感谢您的帮助.
I would like to know if there is a specific tutorial on how to do a POST request with RESTKit. I have looked at some tutorials but I haven't found any that say, "This is exactly how you do a POST request with RESTKit." Help is much appreciated.
推荐答案
假设您已经有一个映射模型,您可以简单地执行以下操作:
Assuming you already have a mapped model, you can simply perform this:
首先,假设您的映射中有一个,则用responseDescriptor
的inverseMapping设置requestDescriptor
.
First, set a requestDescriptor
with the inverseMapping of your responseDescriptor
, assuming you have one with your mapping.
//This is used for mapping responses, you already should have one of this. PS:[Data mapping] is a method that returns an RKObjectMapping for my model. You should create yours or use a previous created one
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Data mapping] pathPattern:nil keyPath:@"data" statusCodes:statusCodeSet];
[[RKObjectManager sharedInstance] addResponseDescriptor:responseDescriptor];
//Inverse mapping, to perform a POST
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[[Data mapping] inverseMapping] objectClass:[Data class] rootKeyPath:nil];
[[RKObjectManager sharedInstance] addRequestDescriptor:requestDescriptor];
在那之后,要执行POST,只需简单地调用下面的方法. Restkit将获取您尝试发布的实例,对其进行序列化并发送到所选的路径.
After that, to perform a POST, just simply call the method below. Restkit will get the instance that you are trying to post, serialize it and send to the path chosen.
[[RKObjectManager sharedInstance] postObject:instanceOfYourModel path:yourPathHere parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error");
}];
如果您没有映射的模型,请告诉我,以便我们可以尝试其他尝试.
If you don't have a mapped model, let me know so we can try something else.
这篇关于RESTKit POST请求教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!