【问题标题】:AFNetworking getting data for XML parse errorAFNetworking 获取 XML 解析错误的数据
【发布时间】:2013-05-22 16:01:57
【问题描述】:

这是我的 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. 为什么会出现此错误?

谢谢。

【问题讨论】:

    标签: objective-c xml xml-parsing afnetworking afhttpclient


    【解决方案1】:
    • Concept of Singleton

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

    您可以查看Singleton Pattern 了解更多信息和示例代码,以强制执行单例的使用方式。

    • 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]]; 
    

    【讨论】:

    • 我应该下载这样的xml数据还是有更简单的方法?我的意思是,这是下载 json 的好方法,但不知道 xml。我用 AFHTTPRequestOperation 替换了单例中的 AFXMLOperation 和我的自定义方法。所以我不想用 AF 解析数据(会用 GDataXML 来做),但这是下载数据的好方法吗? Tnx。
    • 我什至需要在 NSDictionary 中返回它吗?还是应该将整个数据作为 NSArray 返回?
    • 可以使用NSDictionary和用户valueForKey:获取数据。
    • 能否提供xml示例数据。
    • 你需要做什么。
    【解决方案2】:

    我也遇到过类似的问题:

    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)
    

    但是使用AFHTTPRequestOperation下载RSS XML并手动创建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);
    
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 2020-10-04
      • 2017-02-28
      • 2012-05-19
      • 2014-10-19
      • 2013-02-08
      • 2010-10-11
      相关资源
      最近更新 更多