2011-08-24 51 views
0

我正在处理肥皂请求和响应..我能够使服务器请求,我也得到服务器响应,我解析了uialertview消息,但是,当我试图加载它在tabelview,我无法填补的字符串数组,所以,请帮我出如何在iPhone中的表视图加载肥皂响应

下面是我的代码

#import "SoapTableViewController.h" 


@implementation SoapTableViewController 

@synthesize customerArray; 
@synthesize webData; 
@synthesize dict; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


-(void) GetCustomers 
{ 
    NSString *soapMessage [email protected]"<?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" 
    "<GetCustomers xmlns=\"http://www.fashionize.ca/\" />\n" 
    "</soap:Body>\n" 
    "</soap:Envelope>\n";  

    NSURL *url = [NSURL URLWithString:@"http://www.fashionize.ca/Service1.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://www.fashionize.ca/GetCustomers" forHTTPHeaderField:@"SOAPAction"]; 
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if(theConnection) 
    { 
     webData = [[NSMutableData data] retain]; 
    } 
    else 
    { 
     NSLog(@"theConnection is NULL"); 
    } 
} 



-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response 
{ 
    webData =[[NSMutableData data]retain]; 
    [webData setLength: 0]; 
} 


-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{ 
    [webData appendData:data]; 
} 

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{ 
    NSLog(@"Error With Connection"); 
    [webData release]; 
    [connection release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *) connection { 
    NSLog(@"DONE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc] 
         initWithBytes: [webData mutableBytes] 
         length:[webData length] 
         encoding:NSUTF8StringEncoding]; 
    //---shows the XML--- 
    NSLog(theXML); 
    // [self parseXml:theXML]; 
    [theXML release]; 


     if (xmlParser) 
     { 
      [xmlParser release]; 
     }  
     xmlParser = [[NSXMLParser alloc] initWithData: webData]; 
     [xmlParser setDelegate: self]; 
     [xmlParser setShouldResolveExternalEntities:YES]; 
     [xmlParser parse]; 

    [connection release]; 
    [webData release]; 
} 

-(void) parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
    namespaceURI:(NSString *) namespaceURI 
qualifiedName:(NSString *) qName 
    attributes:(NSDictionary *) attributeDict { 

    if([elementName isEqualToString:@"Name"]) 
    { 
     if (!soapResults) 
     { 
      soapResults = [[NSMutableString alloc] init]; 
        } 
     elementFound = YES; 
    } 
} 

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string 
{ 
    if (elementFound) 
    { 
     [soapResults appendString: string]; 

     [customerArray addObject:soapResults]; 

    } 
} 

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
{ 
    if ([elementName isEqualToString:@"Name"]) 
    { 

     //---displays the country--- 
       [customerArray addObject:soapResults]; 
     NSLog(soapResults);   
     elementFound = FALSE; 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle:@"Customer Name!" 


           message:soapResults 
           delegate:self 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
     [soapResults setString:@""]; 
     elementFound = FALSE; 


     //[customerArray addObject:soapResults]; 


    } 
} 

- (void)dealloc 
{ 
    [webData release]; 
    [customerArray release]; 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self GetCustomers]; 
    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [customerArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text =[customerArray objectAtIndex:row]; 
    // Configure the cell... 

    return cell; 
} 

这里u能看到我的客户阵列是空的..所以朋友和极客请告诉我我要去哪里错了..

感谢和问候 Ranjit

回答

0

我没有看到你的客户数组的分配?如果没有它的内存,它不能被添加到哪个将暗示为什么它是空的。

customerArray = [[NSMutableArray alloc]initWithCapacity:0]; 
+0

嗨路易感谢ypour答复......我写的乌尔co​​de..but线随后也充分显示了阵列empty..please帮我出 – Ranjit

+0

朋友你好......任何人可以帮助我这个的........ – Ranjit

0

这可能对你有帮助。在parserDidEndDocument:(NSXMLParser *)parser重新加载你的表格。

-(void)parserDidEndDocument:(NSXMLParser *)parser{ 
[reload tblView]; 
}