3

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.

zhuber
  • 5,364
  • 3
  • 30
  • 63

2 Answers2

3
  • 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]]; 
Community
  • 1
  • 1
icodebuster
  • 8,890
  • 7
  • 62
  • 65
  • Should I even download xml data like this or is there any easier way ? I mean, this is great way of downloading json but dunno for xml. I got it with AFHTTPRequestOperation replacing AFXMLOperation in singleton and in my custom method. So I don't want to parse data with AF (will do it with GDataXML) but is this good way of downloading data ? Tnx. – zhuber May 27 '13 at 20:22
  • Do I even need to return it in NSDictionary ? Or should I return whole data as NSArray ? – zhuber May 27 '13 at 20:24
  • You can use NSDictionary and the user `valueForKey:` to get data. – icodebuster May 27 '13 at 20:26
  • Can you provide xml sample data. – icodebuster May 27 '13 at 20:26
  • What exactly you need to do. – icodebuster May 27 '13 at 20:31
  • Parse that feed to get text, images and stuff for every news. – zhuber May 27 '13 at 20:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30711/discussion-between-icodebuster-and-user1832330) – icodebuster May 27 '13 at 20:32
1

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

}];
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70