2012-05-09 2 views
1

J'utilise le code suivant pour récupérer les données du service Web,Comment afficher les données sur l'affichage du tableau qui sont extraites du service Web dans iphone

- (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>[email protected]</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; 
    } 
} 

Voici les données que je aM obtenant dans la console,

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

Je dois montrer l'nom_table dans le UITableView, j'ai suivi les exemples suivants, http://wwwiphoneresourcesbyemme-rajesh.blogspot.in/2012/04/web-services-example-using-nsxml-parser.html mais je n'a pas pu l'afficher sur le tableView.

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

} 

Toutes les suggestions ou les liens seront appréciés, Merci à l'avance.

+0

être peu specific.where vous êtes face réellement problem.could vous capable d'analyser la réponse? comment vous donnez des données à la vue de la table? – Hariprasad

+0

@ Hariprasad, S'il vous plaît vérifier mon code édité, j'ai ajouté le code de vue de la table. – shasha

Répondre

0

essayer rechargeant uitableview [tableview reloadData]; le moment vous obtenez les données de votre service Web

Questions connexes