0

我在我的应用程序中使用 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>
4

1 回答 1

3

在 viewDidLoad 中初始化块

- (void)viewDidLoad

   {
       chunks = [[NSMutableArray alloc] init];
      //same code here
   }

如果这不起作用。给我来自服务器的数据和来自 xml 的数据的完整日志。

编辑 Rick de Jong:从服务器获取数据时存在一些问题。如果您没有从服务器获取数据,则无法将数据保存在数组中。因此,请从服务器正确获取您的数据。当数据正确地来自服务器时:

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {

    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc]

                    initWithBytes: [webData mutableBytes]

                    length:[webData length]

                    encoding:NSUTF8StringEncoding];



    NSLog(@"Xml is :-----------%@",theXML); // This line should print your xml coming from from server

}

把那个输出给我,之后我一定会给你解决方案。

还有一件事,当您解析数据时。根据您的 Soap,您所需的数据来自标签而不是 xml 的属性。所以你应该使用:

if ( [elementName isEqualToString:@"retLocaResult"] ) {
   // code to save data and add to array
}

在 didStartElement 和 didEndElement 中获得正确的结果。

代码中的另一个错误 remove > 在以下代码中:

NSURL *url = [NSURL URLWithString:

              @"http://www.ctrlbase.nl/cbawebapp/getLoca.asmx"];

最后我得到了输出:

用这个 :

.h 文件

{
NSMutableData *webData;
NSXMLParser *xmlParser;
NSMutableArray *chunks;
BOOL recordResults;

NSMutableString *strCaptured;
}

.m 文件

- (void)viewDidLoad

{

    [super viewDidLoad];

    chunks = [[NSMutableArray alloc] init];

    NSString *temp = @"112";

    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>%@</tcUser></retLoca></soap:Body></soap:Envelope>",temp];

   // 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 *theRequest = [[NSMutableURLRequest alloc] initWithURL:url];

    NSString *msgLength = [[NSString alloc ]initWithFormat:@"%d",[soapMsg length]];

    [theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    //[theRequest addValue:@"http://www.ctrlbase.nl/cbawebapp/retLoca>" forHTTPHeaderField:@"SOAPAction"];

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

    if( theConnection )
    {
        webData = [[NSMutableData alloc] init];            
    }
    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(@"Xml is :-----------%@",theXML);

    if( xmlParser )
    {

    }

    xmlParser = [[NSXMLParser alloc] initWithData:webData];

    xmlParser.delegate = self;

    [xmlParser setShouldResolveExternalEntities:YES];

    [xmlParser parse];

}


- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    strCaptured = [[NSMutableString alloc]init];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    [self.tableView reloadData];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    recordResults = false;
    [strCaptured setString:@""];

    if ([elementName isEqualToString:@"retLocaResult"]) {
        recordResults = true;
    }


}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

    recordResults = false;

    if ([elementName isEqualToString:@"retLocaResult"]){
        [chunks addObject:strCaptured];
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (recordResults
        ) {
        [strCaptured appendString:string];
    }
}


- (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;
}
于 2013-08-08T13:17:42.677 回答