1

我正在使用以下代码从 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;
    }
}

以下是我在控制台中获取的数据,

&lt;normalcontacts&gt;
    &lt;table_id&gt;1788&lt;/table_id&gt;
    &lt;table_name&gt;nareshaug11&lt;/table_name&gt;
    &lt;total_contact&gt;48&lt;/total_contact&gt;
    &lt;created_date&gt;2011-08-11T01:24:00-07:00&lt;/created_date&gt;
  &lt;/normalcontacts&gt;
  &lt;normalcontacts&gt;
    &lt;table_id&gt;1730&lt;/table_id&gt;
    &lt;table_name&gt;nareshtodayjuly28&lt;/table_name&gt;
    &lt;total_contact&gt;2&lt;/total_contact&gt;
    &lt;created_date&gt;2011-07-27T23:01:00-07:00&lt;/created_date&gt;
  &lt;/normalcontacts&gt;
  &lt;normalcontacts&gt;
    &lt;table_id&gt;1685&lt;/table_id&gt;
    &lt;table_name&gt;naresh2&lt;/table_name&gt;
    &lt;total_contact&gt;0&lt;/total_contact&gt;
    &lt;created_date&gt;2011-07-07T22:12:00-07:00&lt;/created_date&gt;
  &lt;/normalcontacts&gt;
  &lt;normalcontacts&gt;
    &lt;table_id&gt;1596&lt;/table_id&gt;
    &lt;table_name&gt;todaymay26&lt;/table_name&gt;
    &lt;total_contact&gt;0&lt;/total_contact&gt;
    &lt;created_date&gt;2011-05-26T03:24:00-07:00&lt;/created_date&gt;
  &lt;/normalcontacts&gt;
  &lt;normalcontacts&gt;
    &lt;table_id&gt;667&lt;/table_id&gt;
    &lt;table_name&gt;my contacts&lt;/table_name&gt;
    &lt;total_contact&gt;1&lt;/total_contact&gt;
    &lt;created_date&gt;2010-07-19T23:13:00-07:00&lt;/created_date&gt;
  &lt;/normalcontacts&gt;

我需要在 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;

}

任何建议或链接将不胜感激,在此先感谢。

4

1 回答 1

0

[tableview reloadData];尝试从 Web 服务获取数据时重新加载 uitableview

于 2012-05-10T10:02:30.150 回答