I have an iOS app that is calling a web service for some XML data as such:
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[xmlParser setDelegate:self];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
I kept getting an error:
Error parsing XML: The operation couldn’t be completed.
(NSXMLParserErrorDomain error 31.)
Error 31 is “NSXMLParserUnknownEncodingError”. I sniffed the HTTP response in WireShark and it turns out the web service is sending “Content-Encoding: gzip” with GZIP’d data in the body and the reason it’s doing that is because the request being sent from iOS sets “Accept-Encoding: gzip, deflate” in the HTTP headers. It seems the initWithContentsOfURL tells webservers it can handle gzip’d data when it actually can’t.
I’m guessing I need to load the web server response into NSData then run it through a decompressor method to get a NSString and then pass that string to the NSXMLParser.
My question is, what’s the best way to do this decompression? ZLib? Any issues linking to ZLib from within iOS?
Thank you,
Stateful
P.S. I tagged this with osx too since I imagine this problem would be there as well, since NSXMLParser is a Foundation class.
Use NSURLConnection delegate methods to download response from webservice. then pass the data to NSXMLParser
initWithContentsOfData.By using NSURLConnection delegate methods (asynchronous connection) you are not blocking the current thread. This is recommended way to download response. Plus NSURLConnection uncompressses(deflates) g-zip compressed response. you don’t need to do anything special to deflate it.
And you can inspect the data before giving it to NSXMLParser ( Thanks Steven ).
Using NSXMLParser initWithContentsOfURL should be mostly used to parse XML files within your Application (in your resources or documents sections). It surely makes connection with your webservice to download response BUT it is equivalent to synchronous call and blocks your thread.
Avoid NSURLConnection sendSynchronous and initWithContentsOfURL to pass remote webservice URL.