使用RestKit将数据POST到服务器,但它不起作用

时间:2013-12-02 09:55:20

标签: restkit

我想使用RestKit将数据发布到服务器。所以我根据https://github.com/RestKit/RestKit编写代码。

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Post class]];
[responseMapping addAttributeMappingsFromArray:@[@"code", @"error",@"bookingNmbr"]]; 
//code,error,bookingNmbr are the parameters returned by server if i post data successfully

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *appliersDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:statusCodes];

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; 
[requestMapping addAttributeMappingsFromArray:@[@"a", @"b", @"dcmnNmbr",@"type"]];
//a,b,dcmnNmbr,type are the parameters what i want to post to the server (into a json file). The structure of the json file is :{"appliers":[{"a":"F","b":9,"dcmnNmbr":"b","type":"c"}]}

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Post class] rootKeyPath:@"appliers" method:RKRequestMethodAny];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:urlPost]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:appliersDescriptor];

Post *post = [Post new];
post.a = @"aa";
post.b = 11;
post.dcmnNmbr = @"bb";
post.type = @"cc";

NSDictionary* params = [NSDictionary dictionaryWithObject:post forKey:@"appliers"];

[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

[manager postObject:params path:urlPost parameters:nil success:nil failure:nil];

以下是结果(代码:400)。它说结构有问题:“找不到应用程序”。

E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0xc8bd5b0 {NSLocalizedRecoverySuggestion={"code":-400,"error":"appliers Not Found"},

那么请你说出它有什么问题。

0 个答案:

没有答案