Restkit响应在text / xml中

时间:2013-02-14 14:00:24

标签: xml restkit mime-types

我有一个返回plist的WS 我正在使用Restkit,我想映射响应。

首先,我首先初始化我的ObjectManager:

sharedInstance.manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:ROOT_URL]];

我接受text / xml:

[[RKObjectManager sharedManager] setAcceptHeaderWithMIMEType:RKMIMETypeTextXML];

我发起了我的请求:

NSMutableURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodPOST path:@"/foo/foo" parameters:nil];
RKManagedObjectRequestOperation *operation = [[RKObjectManager sharedManager] managedObjectRequestOperationWithRequest:request managedObjectContext:[BddManager sharedInstance].manager.managedObjectStore.mainQueueManagedObjectContext success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSLog(@"Loading mapping result: %d", result.count);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Fail!");
}];
[operation start];

最后我收到了这个错误:

NSLocalizedDescription=Expected content type {(
"application/x-www-form-urlencoded",
"application/json"
)}, got text/xml, 

我做错了什么?

3 个答案:

答案 0 :(得分:10)

RestKit 0.20.0rc1在主存储库中不包含XML序列化程序,但您可以在此处找到一个:RKXMLReaderSerialization

通过cocoapods安装:(或将源文件添加到项目中)

pod 'RKXMLReaderSerialization', :git => 'https://github.com/RestKit/RKXMLReaderSerialization.git', :branch => 'master'

导入初始化RestKit的标题。

#import "RKXMLReaderSerialization.h"

最后,使用RestKit注册序列化类。在初始化对象管理器之后和设置接受标头之前插入它。

sharedInstance.manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:ROOT_URL]];
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
[[RKObjectManager sharedManager] setAcceptHeaderWithMIMEType:RKMIMETypeTextXML];

答案 1 :(得分:2)

在官方的RestKit gihub页面上查看此链接。它定于1.0

https://github.com/RestKit/RestKit/issues/1243

答案 2 :(得分:0)

1)您需要告诉RKObjectManager如何处理加载过程后序列化的数据。

[objectManager setRequestSerializationMIMEType:RKMIMETypeTextXML];

修改

刚看到你在谈论一个请求,但你使用的方法是通过POST将对象发送到你的WS。

要从服务器获取对象,请使用RestKit的示例应用程序中提供的以下方法。

[[RKObjectManager sharedManager] getObjectsAtPath:@"yourPath" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    // Success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    // Error
    RKLogError(@"Load failed with error: %@", error);
}];