Skip to content

Instantly share code, notes, and snippets.

@cobysy
Last active August 29, 2015 14:04
Show Gist options
  • Save cobysy/7d30b9fa850698fd5702 to your computer and use it in GitHub Desktop.
Save cobysy/7d30b9fa850698fd5702 to your computer and use it in GitHub Desktop.
An XML and HTML response serializer for AFNetworking 2.0, using GDataXML-HTML
//
// AFNetworkingGDataXMLHTMLResponseSerializer.m
//
// Created by _ on 17/07/14.
// Copyright (c) 2014 cobysy. All rights reserved.
//
#import "AFNetworkingGDataXMLHTMLResponseSerializer.h"
#import <GDataXML-HTML/GDataXMLNode.h>
@implementation AFNetworkingGDataXMLHTMLResponseSerializer
- (id)responseObjectForResponse:(NSURLResponse*)response data:(NSData*)data error:(NSError* __autoreleasing*)error
{
// Validate http response
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if (![self validateResponse:httpResponse data:data error:error])
if ([*error code] == NSURLErrorCannotDecodeContentData)
return nil;
// Convert for example UTF-8 to NSUTF8StringEncoding
NSStringEncoding nsEncoding = NSUTF8StringEncoding; // default to UTF8
if (response.textEncodingName) {
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
nsEncoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
}
NSString* html = [[NSString alloc] initWithData:data encoding:nsEncoding];
NSError* gDataError;
GDataXMLDocument* doc = [[GDataXMLDocument alloc] initWithHTMLString:html encoding:NSUTF8StringEncoding error:&gDataError];
*error = gDataError;
return doc;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment