如何将SOAP响应放入UITableView?

时间:2013-08-05 01:54:13

标签: ios soap

我在我的应用中使用SOAP webservices。我使用以下语句(.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'崩溃。

LOG:

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&amp;携带 2013-08-09 00:12:32.028 Ctrlbase [776:c07]日志输出(null) 2013-08-09 00:12:32.159 Ctrlbase [776:c07] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* - [__ NSArrayM insertObject:atIndex:]:对象不能是零'

-

服务器请求:

    <?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>

1 个答案:

答案 0 :(得分: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

}

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

当您解析数据时,还有一件事。根据您的肥皂,您所需的数据不是作为xml的属性进入标签。所以你应该使用:

if ( [elementName isEqualToString:@"retLocaResult"] ) {
   // code to save data and add to array
}
在didStartElement和didEndElement中获得正确的结果。

代码中的一个错误删除&gt; 在以下代码中:

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