views:

25

answers:

1

hi friends

In my contact management app i am using webservice to get a large xml data, that contains contact details. i use NSURLConnection class for sending requests. But i am facing problem in while getting the XML . First i get a broken XML and then I am getting the whole XML data . Can anyone figure out what is going wrong in my app. this is my piece of code that i am using.

NSData *postData = [postFields dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[[NSString alloc] initWithString:Url]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
conn = nil;
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

and

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[myData appendData:data];

NSString *theXml = [[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];

UIAlertView *thealert = [[UIAlertView alloc] initWithTitle:@"the xml" message:theXml delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[thealert show];
[thealert release];

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser parse];
    [xmlParser release];        
}
+1  A: 

First of all, you really should read Using NSURLConnection in the URL Loading System Programming Guide.

The gist is, connection:didReceiveData: might (and usually does) get called multiple times, so you should do all your processing in connectionDidFinishLoading:.

Can Berk Güder
thanx... Can.. i went thru the documentation .. and the problem have been solved . I invoked the parser from connectionDidFinishLoading:conn method and its now working...