I have an outdated application which use to download an XML document and parse it on the iPhone app, I used the NSURLConnection
for that purpose:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//NSLog(@"Response :%@",response);
responseData = [[NSMutableString alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *str = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
[responseData appendString:str];
[str release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DATA : %@",responseData);
if (responseData != nil) {
[self startParsing:responseData];//Parse the data
[responseData release];
}
}
Since moving to use NSXMLParserDelegate
with AFXMLRequestOperation
, I cannot figure out a way to get xml data properly:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
[responseData appendString:elementName];
[responseData appendString:namespaceURI];
[responseData appendString:qName];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[responseData appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
[responseData appendString:elementName];
[responseData appendString:namespaceURI];
[responseData appendString:qName];
}
-(void) parserDidEndDocument:(NSXMLParser *)parser{
[SVProgressHUD showSuccessWithStatus:@"Downloading completed"];
NSLog(@"DATA : %@",responseData);//not properly appended, tags delimeters are missing
if (responseData != nil) {
[self startParsing:responseData];
[responseData release];
}
}
How to append all the data received from the server in the responseData
mutable string ? I debugged the data received after finishing downloading and the xml is missing tags delimeters <>
. I think I ma missing the way to get the xml data.
P.S: Please note it's important that I get the xml in a NSMutableString
object.
@Fermi
I used AFURLConnectionOperation
as you recommended, it works fine with my purpose, but I noticed that my received data is not catched by the delegate methods, instead I can get the data in a completion block:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:API_URL]];
AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];
operation.completionBlock = ^{
NSLog(@"Complete: %@",operation.responseString);//responseString is my data
};
[operation start];
[SVProgressHUD showWithStatus:@"Downloading files"];
wo since NSURLConnection
delegate methods are not called, how can I manage failure, etc? Thanx.