我正在使用以下代码从 Web 服务中获取数据,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
recordResults = FALSE;
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<GetContacts xmlns=\"http://tempuri.org/\">\n"
"<loginid>nareshreddyyaradla.net@gmail.com</loginid>\n"
"</GetContacts>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",loginId.text
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://www.xxxxxx.net/services/contacts.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/GetContacts" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d",[urlResponse statusCode]);
if([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
{
NSLog(@"Response: %@", result);
}
if( theConnection )
{
webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSURLResponse *)response
{
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConnection");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
if( xmlParser )
{
}
xmlParser = [[NSXMLParser alloc] initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"GetContactsResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = TRUE;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if( recordResults )
{
[soapResults appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"GetContactsResult"])
{
recordResults = FALSE;
greeting.text = soapResults;
soapResults = nil;
}
}
以下是我在控制台中获取的数据,
<normalcontacts>
<table_id>1788</table_id>
<table_name>nareshaug11</table_name>
<total_contact>48</total_contact>
<created_date>2011-08-11T01:24:00-07:00</created_date>
</normalcontacts>
<normalcontacts>
<table_id>1730</table_id>
<table_name>nareshtodayjuly28</table_name>
<total_contact>2</total_contact>
<created_date>2011-07-27T23:01:00-07:00</created_date>
</normalcontacts>
<normalcontacts>
<table_id>1685</table_id>
<table_name>naresh2</table_name>
<total_contact>0</total_contact>
<created_date>2011-07-07T22:12:00-07:00</created_date>
</normalcontacts>
<normalcontacts>
<table_id>1596</table_id>
<table_name>todaymay26</table_name>
<total_contact>0</total_contact>
<created_date>2011-05-26T03:24:00-07:00</created_date>
</normalcontacts>
<normalcontacts>
<table_id>667</table_id>
<table_name>my contacts</table_name>
<total_contact>1</total_contact>
<created_date>2010-07-19T23:13:00-07:00</created_date>
</normalcontacts>
我需要在 UITableView 中显示 table_name,我遵循以下示例, http://wwwiphoneresourcesbyemme-rajesh.blogspot.in/2012/04/web-services-example-using-nsxml-parser.html 但我无法将其显示在 tableView 上。
表视图代码,
- (void)parserDidEndDocument:(NSXMLParser *)parser{
UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
tab.delegate = self;
tab.dataSource = self;
[self.view addSubview:tab];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [namedata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text=[arrareasdata objectAtIndex:indexPath.row];
cell.imageView.image=[array objectAtIndex:indexPath.row];
tableView.separatorColor=[UIColor greenColor];
cell.textLabel.textColor=[UIColor blackColor];
return cell;
}
任何建议或链接将不胜感激,在此先感谢。