我想使用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 :(得分:0)
这一行:
NSDictionary* params = [NSDictionary dictionaryWithObject:post forKey:@"appliers"];
不是必需的(你实际上并没有使用)。
您显示的JSON是字典中的Post对象数组。目前你应该在字典中找到一本字典。您可以使用Charles或类似工具进行检查。
要获取您正在寻找的数组输出,请发布一个数组:
[manager postObject:@[ post ] path:urlPost parameters:nil success:nil failure:nil];