2010-07-14 83 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