我在我的应用程序中使用 SOAP 网络服务。我使用以下语句(.m 文件)提出请求:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soapMsg =
[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?><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/>\"><soap:Body><retLoca xmlns=\"http://www.ctrlbase.nl/cbawebapp>\"><tcUser>112</tcUser></retLoca></soap:Body></soap:Envelope>"];
NSURL *url = [NSURL URLWithString:
@"http://www.ctrlbase.nl/cbawebapp/getLoca.asmx>"];
NSMutableURLRequest *req =
[NSMutableURLRequest requestWithURL:url];
NSString *msgLength =
[NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://www.ctrlbase.nl/cbawebapp/retLoca>"
forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength
forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.labelText = @"Ophalen van gegevens";
HUD.delegate = self;
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if( theConnection )
{
webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
}
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(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);
if( xmlParser )
{
}
xmlParser = [[NSXMLParser alloc] initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[HUD hide:YES];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ( [elementName isEqualToString:@"retLocaResponse"] ) {
NSLog(@"Log Output%@",[attributeDict objectForKey:@"retLocaResult"]);
NSString *strValue= [attributeDict objectForKey:@"retLocaResult"];
if(strValue != (NSString*)[NSNull null])
{
[chunks addObject:[attributeDict objectForKey:@"retLocaResult"]];
}
else
{
}
recordResults = YES;
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ( [elementName isEqualToString:@"retLocaResponse"] ) {
NSLog(@"Log array%@",chunks);
[[self tableView]reloadData];
recordResults = NO;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [chunks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
cell.textLabel.text =[chunks objectAtIndex:indexPath.row];
return cell;
}
但我没有将位置名称放入 UITableView。我做错了什么或者我能做些什么来让它工作。请帮助我,以便我可以完成我的申请。
非常感谢!
Samkit Jain 的编辑:与数组的“init”一起崩溃。
日志:
2013-08-09 00:12:31.631 Ctrlbase[776:c07] 请求完成 2013-08-09 00:12:31.904 Ctrlbase[776:c07] 完成。接收字节数:381 2013-08-09 00:12:31.904 Ctrlbase[776:c07] 0640. Home5 Cash & Carry 2013-08-09 00:12:32.028 Ctrlbase[776:c07] 日志输出(空)2013-08 -09 00:12:32.159 Ctrlbase[776:c07] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[__NSArrayM insertObject:atIndex:]: object cannot be nil”
-
服务器请求:
<?xml version="1.0" encoding="utf-8"?>
<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/">
<soap:Body>
<retLoca xmlns="http://www.ctrlbase.nl/cbawebapp">
<tcUser>string</tcUser>
</retLoca>
</soap:Body>
</soap:Envelope>
服务器响应:
<?xml version="1.0" encoding="utf-8"?>
<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/">
<soap:Body>
<retLocaResponse xmlns="http://www.ctrlbase.nl/cbawebapp">
<retLocaResult>string</retLocaResult>
</retLocaResponse>
</soap:Body>
</soap:Envelope>