代码之家  ›  专栏  ›  技术社区  ›  Sabby

Web服务n sqlite

  •  -2
  • Sabby  · 技术社区  · 15 年前

    请帮助我,并提供完整的代码如何做到这一点。

    1 回复  |  直到 15 年前
        1
  •  3
  •   jfalexvijay    15 年前

    Webservices可以是Java、PHP、.NET等语言。。。但你必须用同样的程序来提出请求。 在这里,我给出了示例代码,用于发出请求并从webservices获取响应。

    
    -(void)performRequest{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url"]];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
        [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request addValue: soapAction forHTTPHeaderField:@"SOAPAction"];
        [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if( theConnection )
        {
            webData = [[NSMutableData data] retain];
        }
        else
        {
            NSLog(@"theConnection is NULL");
        }
        [pool release];
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        [webData setLength: 0];
        self.resultArray = [[NSMutableArray alloc] init];
    }
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        [webData appendData:data];
    }
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        NSLog(@"ERROR with theConenction");
        NSDictionary *errorDic = [NSDictionary dictionaryWithObject:error forKey:@"error"];
        [self.resultArray addObject:errorDic];
        [connection release];
        [webData setLength:0];
    }
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
        NSLog(@"DONE. Received Bytes: %d", [webData length]);
        NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
        NSLog(@"%@", theXML);
        [theXML release];
        if([webData length] > 0){
            parser = [[NSXMLParser alloc] initWithData:webData];
            [parser setDelegate:self];
            [parser parse]; 
        }
    }
    
    

    有一些委托方法。你必须使用以下指定的方法;

    
    1. - (void)parserDidStartDocument:(NSXMLParser *)parser
    2. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
    3. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    4. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    5. - (void)parserDidEndDocument:(NSXMLParser *)parser
    6. - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError

    在第二个委托方法中,您将得到元素名(xml标记名)。 在第三个委托方法中,您将获得元素名的值。

    推荐文章