Restkit无法将请求序列化为xml

时间:2013-11-30 16:45:29

标签: ios xml restkit restkit-0.20

我使用RestKit 0.20.0来处理API(xml)。

但是当我尝试进行XML POST请求和设置映射时,请求正文总是null

RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://url:4465/paci/v1.0"]];

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
objectManager.requestSerializationMIMEType = RKMIMETypeXML;
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeXML];

映射:

RKObjectMapping *createServerMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[createServerMapping addAttributeMappingsFromDictionary:@{ @"name": @"name",
                                                           @"description": @"description",
                                                           @"ramSize": @"ram-size",
                                                         }];

RKRequestDescriptor *createServerDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:createServerMapping objectClass:[NSMutableDictionary class] rootKeyPath:@"ve" method:RKRequestMethodPOST];

[objectManager addRequestDescriptor:createServerDescriptor];

对象:

IBCreateServer *createServerRequest = [[IBCreateServer alloc] init];
createServerRequest.name = @"web40";
createServerRequest.description = @"testtest";
createServerRequest.ramSize = @"512";

发布对象:

[objectManager postObject:createServerRequest path:@"ve" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {} failure:^(RKObjectRequestOperation *operation, NSError *error) {}];

此请求的结果是:

request.headers={
    Accept = "application/xml";
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
}
request.body=(null)

Body始终为null。我做错了什么?只有请求才会发生这种情况,响应XML解析才能正常工作。

编辑:如果我序列化为json,它可以正常工作

1 个答案:

答案 0 :(得分:2)

您的请求描述符错误。它应该是:

RKRequestDescriptor *createServerDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:createServerMapping objectClass:[IBCreateServer class] rootKeyPath:@"ve" method:RKRequestMethodPOST];

这告诉RestKit你的源类是什么以及当你要求它发布该类的实例时它应该使用什么映射。映射告诉RestKit将实例转换为什么(一个可变字典),以便它可以串行化为XML。