Web服务数据未在iphone中的NSXMLParser中加载

时间:2013-05-27 14:01:38

标签: iphone ios objective-c

我是手机编程的新手。我从webservice获得了一些回复。但它没有在nsxmlparser中加载这些数据。

导入“ViewController.h”

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,NSXMLParserDelegate,NSURLConnectionDelegate>
{

    NSMutableData *webData;
    NSMutableString *soapResults;
    NSXMLParser *xmlParser;
    BOOL xmlResults;
    NSMutableArray *arr_code,*arr_name;
}
@property(nonatomic,retain)NSMutableArray *arr_code,*arr_name;
@property(nonatomic,retain)NSMutableData *webData;
@property(nonatomic,retain)NSMutableString *soapResults;
@property(nonatomic,retain)NSXMLParser *xmlParser;

导入“ViewController.h”

@synthesize arr_code,arr_name,webData,soapResults,xmlParser,nb,table;
- (void)viewDidLoad
{
    arr_code=[[NSMutableArray alloc]init];
    arr_name=[[NSMutableArray alloc]init];
    xmlResults =FALSE;
     NSString *soapMessage=[NSString stringWithFormat:
                           @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                           "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                           "<soap:Body>\n"

                           "<OnlineStatus xmlns=\"http://tempuri.org/\">\n"
                           "<CafeName>Cyber Cafe Name</CafeName>\n"
                           "<FromDate>2012-03-01</FromDate>\n"
                           "<ToDate>2013-05-03</ToDate></OnlineStatus>\n"
                           "</soap:Body>\n"

                           "</soap:Envelope>"];
    NSLog(@"%@",soapMessage);

    NSURL *url = [NSURL URLWithString:@"http://www.ebidmanagerdemo.com/gjHouseOnline/xml/XMLDataService.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://tempuri.org/OnlineStatus" 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];
    }
    else {
        NSLog(@"theConnection is NULL");

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    NSLog(@"%@",msgLength);
            [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }
}
-(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(@"ERROR with theConnenction");
    }

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    theXML = [theXML stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    theXML = [theXML stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    theXML = [theXML stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
       NSLog(@" getting in xml %@",theXML);
    NSLog(@"XMl is workinggggggggg");
  xmlParser = [[NSXMLParser alloc]initWithData:webData];
    NSXMLParser *aslamxml =[[NSXMLParser alloc]initWithData:dataa];  
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser setDelegate: self];
    [xmlParser parse];
    NSLog(@" iiiiii %@",xmlParser);
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict
{
    NSLog(@"fsdfsd");
    if( [elementName isEqualToString:@"MachineIP"])
    {
      if(!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];

        }
        xmlResults = YES;

    }
    else
    {
        if( [elementName isEqualToString:@"MachineIP"])
        {


            if(!soapResults)
            {
                soapResults = [[NSMutableString alloc] init];

            }
            xmlResults = YES;

        }
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if( xmlResults )
    {
        [self.soapResults appendString: string];
        NSLog(@"soap result %@",soapResults);

    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if( [elementName isEqualToString:@"MachineIP"])
    {
        xmlResults = FALSE;
        [arr_code addObject:soapResults];
       // NSLog(@"hello %@",arr_code);

        soapResults = nil;
    }
    else {
        if( [elementName isEqualToString:@"MachineIP"])
        {
            xmlResults = FALSE;
            [arr_name addObject:soapResults];
            NSLog(@"hiii %@",arr_name);

            soapResults = nil;
        }
    }
    [table reloadData];

}

我得到这样的输出 如果我给NSLog(@“get in xml%@”,theXML); 在控制台中显示下面的想法

2013-05-27 19:13:35.735 NewC Newcafezee[1202:11303] DONE. Received Bytes: 18345
2013-05-27 19:13:35.737 NewC Newcafezee[1202:11303]  getting in xml <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><OnlineStatusResponse xmlns="http://tempuri.org/"><OnlineStatusResult><?xml version="1.0" encoding="utf-8"?>
<DataSet>
  <xs:schema id="Result" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Result" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="SystemDateTime" type="xs:dateTime" minOccurs="0" />
                <xs:element name="CafeDateTime" type="xs:dateTime" minOccurs="0" />
                <xs:element name="LicenseNumber" type="xs:string" minOccurs="0" />
                <xs:element name="MachineIP" type="xs:string" minOccurs="0" />
                <xs:element name="TotalClients" type="xs:int" minOccurs="0" />
                <xs:element name="IdleClients" type="xs:int" minOccurs="0" />
                <xs:element name="BusyClients" type="xs:int" minOccurs="0" />
                <xs:element name="DummyIdle" type="xs:int" minOccurs="0" />
                <xs:element name="DummyBusy" type="xs:int" minOccurs="0" />
                <xs:element name="PCUsageSessions" type="xs:int" minOccurs="0" />
                <xs:element name="PCUsageMinutes" type="xs:int" minOccurs="0" />
                <xs:element name="SessionMessage" type="xs:string" minOccurs="0" />
                <xs:element name="CreatedBy" type="xs:int" minOccurs="0" />
                <xs:element name="CreatedDate" type="xs:dateTime" minOccurs="0" />
                <xs:element name="Cybercafe" type="xs:string" minOccurs="0" />
                <xs:element name="Email" type="xs:string" minOccurs="0" />
                <xs:element name="AdminPassword" type="xs:string" minOccurs="0" />
                <xs:element name="MajorVersion" type="xs:short" minOccurs="0" />
                <xs:element name="MinorVersion" type="xs:short" minOccurs="0" />
                <xs:element name="SalesAmount" type="xs:decimal" minOccurs="0" />
                <xs:element name="PurchasesAmount" type="xs:decimal" minOccurs="0" />
                <xs:element name="Revision" type="xs:int" minOccurs="0" />
                <xs:element name="CZLicense" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <Result>
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <SystemDateTime>2012-09-20T00:00:00-06:00</SystemDateTime>
        <CafeDateTime>2012-09-18T11:01:54-06:00</CafeDateTime>
        <LicenseNumber>cc</LicenseNumber>
        <MachineIP>192.168.13.167</MachineIP>
        <TotalClients>0</TotalClients>
        <IdleClients>0</IdleClients>
        <BusyClients>0</BusyClients>
        <DummyIdle>0</DummyIdle>
        <DummyBusy>0</DummyBusy>
        <PCUsageSessions>0</PCUsageSessions>
        <PCUsageMinutes>0</PCUsageMinutes>
        <SessionMessage>LVCLPRLbSCTC</SessionMessage>
        <CreatedBy>1</CreatedBy>
        <CreatedDate>2012-09-20T00:00:00-06:00</CreatedDate>
        <Cybercafe>Cyber Cafe Name</Cybercafe>
        <Email>YOURMAIL@CAFE.COM</Email>
        <AdminPassword>admin1</AdminPassword>
        <MajorVersion>1</MajorVersion>
        <MinorVersion>0</MinorVersion>
        <SalesAmount>0.0000</SalesAmount>
        <PurchasesAmount>0.0000</PurchasesAmount>
        <Revision>0</Revision>
        <CZLicense />
      </Table>
    </Result>
  </diffgr:diffgram>
</DataSet></OnlineStatusResult></OnlineStatusResponse></soap:Body></soap:Envelope>

但是如果给予  NSLog(@“iiiiii%@”,xmlParser);它是为XMLParser而不是在NSXMlparser中加载它显示为空可以任何正文告诉我这段代码中的错误是什么?我不知道为什么它没有加载到NSXMLparser中。

2013-05-27 19:13:35.738 NewC Newcafezee[1202:11303] XMl is workinggggggggg
2013-05-27 19:13:35.739 NewC Newcafezee[1202:11303]  iiiiii <NSXMLParser: 0x7191d20>

我收到的错误代码有些想法,如下所示

2013-05-27 19:43:35.962 NewC Newcafezee[1291:11303] error parsing XML: Unable to download story feed from web site (Error code 64 ) http://www.ebidmanagerdemo.com/gjHouseOnline/xml/XMLDataService.asmx

在上面的输出中,我得到两次代码

<?xml version="1.0" encoding="utf-8"?>

这是错误ra的主要原因?请告诉我什么是错误?

3 个答案:

答案 0 :(得分:0)

您已在soapResults数组中设置了值。尝试使用以下方法打印它: parserDidEndDocument

- (void)parserDidEndDocument:(NSXMLParser *)parser

答案 1 :(得分:0)

首先知道错误

-(void)parser:(NSXMLParser*)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"error:%@",parseError);

}

答案 2 :(得分:0)

didEndElement的变化xmlResults = FALSE;到xmlResults = NO;