2

我正在尝试通过 RESTKit 使用 XML Web 服务。

我能够连接到该服务,但收到如下错误消息:

error=Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type (null), got application/xml"

我尝试如下设置内容类型标头,但仍未得到任何更改:

AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client setDefaultHeader:@"Content-Type" value:RKMIMETypeXML];

希望有人可以提供帮助。

我已经包含了我用来发出以下请求的代码:

NSURL *baseURL = [NSURL URLWithString:@"http://someaddress/"];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
[objectManager setRequestSerializationMIMEType:RKMIMETypeXML];
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeXML];

RKObjectMapping *recipeMapping = [RKObjectMapping mappingForClass:[Recipe class]];
[recipeMapping addAttributeMappingsFromDictionary:@{
 @"articleid" : @"ArticleId",
 @"title" : @"Title",
 @"url" : @"URL"
 }];


// Register our mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:recipeMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"Recipes"
                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];

[objectManager getObjectsAtPath:@"some/path/"
                     parameters:nil
                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                            NSArray *recipes = [mappingResult array];
                            NSLog(@"Loaded statuses: %@", recipes);
                        }
                        failure:^(RKObjectRequestOperation *operation, NSError *error) {
                            NSLog(@"Hit error: %@", [error localizedDescription]);
                        }];

问题是我仍然得到预期的内容类型(null),得到应用程序/xml 错误消息。

此外,我通过一个非常快速和简单的示例 ASIHTTP 应用程序调用了服务 URL,这很成功。我一定错过了一些基本的东西。

谢谢

4

1 回答 1

0

就像 Martin 提到的,此时您不需要直接与 AFHTTPClient 对话。

RKObjectManager* manager = [RKObjectManager managerWithBaseURL:baseURL];
[manager setRequestSerializationMIMEType:@"application/xml"];
[manager setAcceptHeaderWithMIMEType:@"application/xml"];
于 2012-12-10T09:13:50.137 回答