2010-07-14 40 views
0

在我的联系人管理应用程序中,我使用webservice获取包含联系人详细信息的大型xml数据。我使用NSURLConnection类来发送请求。但是我在获取XML时遇到问题。首先,我得到一个破损的XML,然后我得到了整个XML数据。任何人都可以弄清楚我的应用程序出了什么问题。这是我使用的一段代码。从webservice获取破损的XML,然后是完整的XML

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]; 

-(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];   
} 

回答

3

首先,你真的应该阅读URL Loading System Programming GuideUsing NSURLConnection

要点是,connection:didReceiveData:可能(通常会)被多次调用,因此您应该在connectionDidFinishLoading:中执行所有处理。

+0

thanx ...可以..我去通过文件..和问题已被解决。我从connectionDidFinishLoading:conn方法调用了解析器,现在它正在工作...... – user347161 2010-07-14 12:43:40