Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 831
zhuber Avatar asked Mar 11 '26 07:03

zhuber


2 Answers

  • Concept of Singleton

    A singleton is more commonly known as a design pattern. Usually a singleton is a class and behaves exactly like any other class, the only exception being that any instances of a singleton reference the same object data. This means that any instance of a singleton class are actually all the same instance.

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

  • Is there any suggestion if whole concept is wrong ?

    I would suggest you to use Singleton for AFNetworking since you will have only one instance of it.

  • Your Error

    The error you are getting is because AFNetworking request wants Header Content-Type as "application/xml" or "text/xml"

Try changing this code:

[self registerHTTPOperationClass:[AFXMLRequestOperation class]]; 

to

[self registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 
like image 149
icodebuster Avatar answered Mar 13 '26 21:03

icodebuster


I had a similar problem:

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

The answer above is not full and clear, although it helped me a lot after I read their chat. registerHTTPOperationClass doesn't help. I decided to provide some code. Solution is to NOT use this:

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

But download RSS XML using AFHTTPRequestOperation and create NSXMLParser manually:

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);

}];
like image 28
Denis Kutlubaev Avatar answered Mar 13 '26 21:03

Denis Kutlubaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!