开发者

Obtaining broken XML followed by full XML from webservice

开发者 https://www.devze.com 2023-01-07 07:09 出处:网络
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 th

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 dataUsingEncodi开发者_C百科ng: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];        
}


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:.

0

精彩评论

暂无评论...
验证码 换一张
取 消