objective-cxmlxml-parsingafnetworkingafhttpclient

AFNetworking getting data for XML parse error


This is my AFHTTPClient singleton:

+ (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;
}

And method in same class (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];
}

When I call this function to get XML from RSS I get this error:

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

Question:

  1. Is whole concept of implemented singleton good and do I need any changes ?

  2. Is there any suggestion if whole concept is wrong ?

  3. Why am I getting this error?

Thanks.


Solution

  • You can check out Singleton Pattern for more information and sample code to enforce how the singleton will be used.

    Try changing this code:

    [self registerHTTPOperationClass:[AFXMLRequestOperation class]]; 
    

    to

    [self registerHTTPOperationClass:[AFHTTPRequestOperation class]];