iPhone:如何通过网络服务获取值数组?

标签 iphone ios web-services ipad nsxmlparser

我在 iPhone 中使用网络服务并通过 NSXMLParser 解析它。我的服务返回对象数组,但是当我从 Web 服务获取它并连接相同类型的值时。 Web 服务生成以下响应。

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <GetClientTripsResponse xmlns="http://tempori.net/">
      <GetClientTripsResult>
        <TripNumbers>
          <ConfirmationsNumber>string</ConfirmationsNumber>
          <TripState>string</TripState>
        </TripNumbers>
        <TripNumbers>
          <ConfirmationsNumber>string</ConfirmationsNumber>
          <TripState>string</TripState>
        </TripNumbers>
      </GetClientTripsResult>
    </GetClientTripsResponse>
  </soap:Body>
</soap:Envelope> 

对于 ConfirmationsNumber 是 6 位数字,TripState 是字符串值 NONE 或 ONWAY

但是当我解析它时,它给出了

ConfirmationsNumber = 234589455623784523 和

TripState = NONENONEONWAY

表示它解析三个对象并将它们连接起来。我希望有孤立的值或至少以逗号分隔。像 234589,455623,784523 之类的,但应该可以拆分。

代码如下

@interface TrackTripStatus : UITableViewController <NSXMLParserDelegate> {
    NSMutableArray *tripList;
    UILabel *noTripsLabel;
    int indexCount;
    BOOL elementFound;

    // for Web service
    NSMutableData *webData;
    NSMutableString *soapResults;
    NSURLConnection *conn;
    NSXMLParser * xmlParser;
    NSString * qelementName;
    NSString * RequestStep;
}

- (void) getClientTrips;
-(void) setControls;
@end

- (void) getClientTrips
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *CLIENTPHONENUMBER = [defaults stringForKey:@"CLIENTPHONENUMBER"];
    if(CLIENTPHONENUMBER == nil)
        CLIENTPHONENUMBER = @"";

    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>"
                         "<GetClientTrips xmlns=\"http://tempori.net/\">"
                         "<HomePhone>%@</HomePhone>"
                         "</GetClientTrips>"
                         "</soap:Body>"
                         "</soap:Envelope>",CLIENTPHONENUMBER]; 

    NSURL *url = [NSURL URLWithString: WebServiceURL];
    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://Itcurves.net/GetClientTrips" forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; //---set the HTTP method and body--- 
    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"%@\n", soapMsg); 
    RequestStep = @"GetClientTripsResult";
    if (conn) {
        [conn release];
    }

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    if (conn) 
    { 
        webData = [[NSMutableData data] retain];
    }
}


-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {

    [webData setLength: 0];
}

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

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
    NSLog(@"Test: %@", error);
    [webData release]; 
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{ 
    NSLog(@".........DONE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc]    //---shows the XML---
                        initWithBytes:[webData mutableBytes] length:[webData length]
                        encoding:NSUTF8StringEncoding];
    NSLog(@"\n\n %@",theXML);
    if([theXML length] < 120)
    {
        if(indexCount < 3)
        {
            if([RequestStep isEqualToString:@"GetClientTripsResult"])
                [self SendClientInfo];
            indexCount = indexCount +1;
        }
        else {
            RequestStep = @"end";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Server was unable to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show]; 
            [alert release];
        }

    }   

    [theXML release];                                                                                       


    /*  CODE TO RELEASE XML start  */
    if (xmlParser) 
    { 
        [xmlParser release];
    }
    [soapResults setString:@""]; 

    xmlParser = [[NSXMLParser alloc] initWithData: webData]; 
    [xmlParser setDelegate:self]; 
    [xmlParser setShouldResolveExternalEntities:YES]; 

    /*  CODE TO RELEASE XML end  */
    NSLog(@"Step: %@, %@",RequestStep, soapResults);
    if([RequestStep isEqualToString:@"GetClientTripsResult"])
    {
        qelementName = @"ConfirmationsNumber";
        [xmlParser parse];
        if ([soapResults length]> 0) 
        {
            NSString * ConformNo = soapResults;
            qelementName = @"TripState";
            [soapResults setString:@""];
            [xmlParser parse];
            if ([soapResults length] > 1) {
                NSLog(@"Step2: %@, %@",RequestStep, soapResults);
                indexCount =0;
                RequestStep = @"end";



            }
            else{               
                qelementName = @"DeclineReason";
                [soapResults setString:@""];
                [xmlParser parse];
                RequestStep = @"end";               
            }
        }
    }   
    if([RequestStep isEqualToString: @"end"])
    {
        //  [connection release];                                                                                                                                    
        //  [webData release];      
    }
}

最佳答案

每当我使用 NSXMLParser 时,我都会使用回调来解析它。这是我为此使用的代码:

首先设置委托(delegate)和一个类变量

@interface FooBarClass : UIViewController <NSXMLParserDelegate>
{
    NSMutableString *currentElementValue;
}

然后在任何需要获取url并设置xmlparser的地方

NSString *address = [[NSString alloc] initWithString:@"http://somewebservice"];
NSURL *url = [[NSURL alloc] initWithString:address];

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

[url release];

[xmlParser setDelegate:self];

[xmlParser parse];
[xmlParser release];

然后是回调:

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

if([elementName isEqualToString:@"Foo"]) {  //start of main element to save
    if (Bar) 
    {
        [Bar release];
        Bar = nil;
    }
    Bar = [BarType alloc];
}

//finding characters in an element. could be multiple calls so append here
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{

if(!currentElementValue)
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];
}

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

NSString *trimmedValue = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if([elementName isEqualToString:@"Foo"])   //this is the end of the top level object so add it to our array
{
    [fooArray addObject:Bar];

}
else if([elementName isEqualToString:@"Foo2"])  //this is one of the features so we set it in our object
{
    Bar.fooBarString = trimmedValue;
}



[currentElementValue release];  //release this to make room for the next element
currentElementValue = nil;
}

关于iPhone:如何通过网络服务获取值数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10168151/

相关文章:

iphone - 添加 .a 文件后体系结构 armv7 的 undefined symbol

iphone - 尝试在从 segue 返回时运行代码

ios - 使用动画动态地将行添加到 TableView 的末尾

Java Web 服务 - 更好的日期选择 - java.util.Date 限制?

iphone - 在 iOS 中创建可平移的 3D 空间

ios - 以流畅的动画展开 UITableViewCell

ios - 确定 `UIView` 是从 XIB 加载还是从代码实例化

ios - OCMock: stub 从不匹配签名

c# - 将大文件流式传输到 Web 服务

java - 如何连接到 Web 服务(启用 SSL 和代理接口(interface))