3

这是我的 AFHTTPClient 单例:

+ (API *)sharedInstance
{
static API *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedInstance = [[API alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
    [sharedInstance setParameterEncoding:AFJSONParameterEncoding];
    [sharedInstance registerHTTPOperationClass:[AFXMLRequestOperation class]];
    [sharedInstance setDefaultHeader:@"Accept" value:@"application/rss+xml"];
});

return sharedInstance;
}

和同一类中的方法(AFHTTPClient):

- (void)requestXMLDataCompletion:(JSONResponseBlock)completionBlock
{
NSMutableURLRequest *apiRequest = [self requestWithMethod:@"GET" path:kAPIPath parameters:nil];

AFXMLRequestOperation *operation = [[AFXMLRequestOperation alloc] initWithRequest:apiRequest];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
    // success
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // failure
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
}

当我调用此函数从 RSS 获取 XML 时,我收到此错误:

error = "Expected content type {(\n    \"application/xml\",\n    \"text/xml\"\n)}, got application/rss+xml";

问题:

  1. 实施单例的整个概念是否良好,我需要任何更改吗?

  2. 如果整个概念是错误的,有什么建议吗?

  3. 为什么我会收到此错误?

谢谢。

4

2 回答 2

3
  • Concept of Singleton

    单例通常被称为设计模式。通常单例是一个类,其行为与任何其他类完全相同,唯一的例外是单例的任何实例都引用相同的对象数据。这意味着单例类的任何实例实际上都是同一个实例。

您可以查看单例模式以获取更多信息和示例代码,以强制执行单例的使用方式。

  • Is there any suggestion if whole concept is wrong ?

    我建议您将 Singleton 用于 AFNetworking,因为您将只有一个实例。

  • Your Error

    您收到的错误是因为 AFNetworking 请求希望 Header Content-Type 为“application/xml”“text/xml”

尝试更改此代码:

[self registerHTTPOperationClass:[AFXMLRequestOperation class]]; 

[self registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 
于 2013-05-27T20:02:23.683 回答
1

我有一个类似的问题:

Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "text/xml",
    "application/xml"
)}, got application/rss+xml"

上面的答案并不完整和清晰,尽管在我阅读他们的聊天后对我有很大帮助。registerHTTPOperationClass没有帮助。我决定提供一些代码。解决方案是不要使用这个:

[AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser)

但是RSS XML使用下载并手动AFHTTPRequestOperation创建:NSXMLParser

NSString *articlesUrlString = @"http://pro.rabota.ru/feed/moscow.content.rss";
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:articlesUrlString]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"" parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

  NSData *xmlData = (NSData *)responseObject;
  NSXMLParser *XMLParser = [[NSXMLParser alloc] initWithData:xmlData];
  XMLParser.delegate = self;
  [XMLParser parse];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  NSLog(@"error: %@", error);

}];
于 2014-10-20T15:17:44.893 回答