1

我正在尝试将 RestKit 0.20 集成到我的项目中,例如,我使用 google rss 阅读器作为示例项目。

这是对 Xml
/ /category> & 的响应

这是我必须从中获取数据的 Xml。

RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[新闻类]]; [articleMapping addAttributeMappingsFromDictionary:@{@"title":@"title",@"link":@"link",@"guid":@"guid",@"pubDate":@"pubDate",@"description" : @“描述” }];

       NSURL* url = [[NSURL alloc]initWithString:@"http://news.google.com"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];

NSURLRequest* request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:@"/?output=rss" parameters:nil];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping pathPattern:nil keyPath:@"item" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];



[objectRequestOperation start];

以上是我用来获取数据的代码当我运行应用程序时出现以下错误

我 restkit.network:RKObjectRequestOperation.m:174 GET ' http://news.google.com/?output=rss ' 2013-05-28 12:24:15.076 TestApp[8940:15203] E restkit.network:RKObjectRequestOperation。 m:569 对象请求失败:基础 HTTP 请求操作失败并出现错误:错误域 = org.restkit.RestKit.ErrorDomain 代码 = -1016 “预期内容类型 {( “application/xml”,“application/x-www-form- urlencoded", "application/json" )}, got application/rss+xml" UserInfo=0xa80a3d0 {NSLocalizedRecoverySuggestion=NFE/1.0Top Stories - Google 新闻

任何人都可以帮助

4

1 回答 1

4

正如例外所说,RestKit 期待以下之一:

  1. “应用程序/xml”
  2. “应用程序/x-www-form-urlencoded”
  3. “应用程序/json”

但它正在接收“应用程序/rss+xml”。你只需要告诉 RestKit 会发生什么。

你可以询问 RestKit 是否已经理解 mime 类型:

[RKMIMETypeSerialization registeredMIMETypes];

如果没有,您需要使用RKMIMETypeSerialization.

你可以告诉对象管理器期望什么 mime 类型:

[objectManager setAcceptHeaderWithMIMEType:@"application/rss+xml"];
于 2013-05-28T08:04:08.620 回答