如何在 iphone 的表视图上加载 Soap 响应
Posted
技术标签:
【中文标题】如何在 iphone 的表视图上加载 Soap 响应【英文标题】:How to load Soap response on table view in iphone 【发布时间】:2011-08-24 14:36:07 【问题描述】:我正在处理soap请求和响应..我能够发出服务器请求并且我得到服务器响应,我在uialertview消息上解析,但是当我试图加载它时在表格视图上,我无法用字符串填充数组,所以请帮帮我
下面是我的代码
#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 =@"<?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;
在这里你可以看到我的客户数组是空的..所以朋友和极客请告诉我哪里出错了..
感谢和问候 兰吉特
【问题讨论】:
【参考方案1】:我没有看到您的客户数组的分配?如果它没有内存,则无法添加它,这会暗示为什么它是空的。
customerArray = [[NSMutableArray alloc]initWithCapacity:0];
【讨论】:
嗨 louie 感谢您的回复...我写了您的代码行..但它也显示数组是空的..请帮帮我【参考方案2】:这可能会对您有所帮助。在parserDidEndDocument:(NSXMLParser *)parser
中重新加载您的表格。
-(void)parserDidEndDocument:(NSXMLParser *)parser
[reload tblView];
【讨论】:
以上是关于如何在 iphone 的表视图上加载 Soap 响应的主要内容,如果未能解决你的问题,请参考以下文章
如何在主视图的行选择上更新详细视图的表视图或在详细视图中加载新视图?
在 iPhone 中,我如何知道在使用正向地理编码时我的所有地图注释何时加载?