【问题标题】:Send Request For Web Service And Important Delegate Methods For XML Parsing发送 Web 服务请求和 XML 解析的重要委托方法
【发布时间】:2011-09-30 11:10:57
【问题描述】:

我想向服务发送请求,作为回复,我正在获取 xml 数据。

那么,请告诉我如何通过检查互联网连接向网络服务器发送请求,并告诉我用于 xml 解析的重要委托方法。

【问题讨论】:

    标签: iphone xml-parsing urlconnection


    【解决方案1】:

    首先您还需要获取 Reachability.h 和 Reachability.m 文件

    *您可以从这里获取可达性文件:-- *

    http://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

    还需要在您要检查互联网连接的文件中导入 .h 文件

    在您的 .h 文件中定义以下变量

    NSMutableData *urlData;
    NSMutableString *currentElementValue;
    

    同时定义NSMutableString的属性

    下面是urlConnection的代码

        reachability = [Reachability reachabilityForInternetConnection];
        [reachability startNotifier];
    
        NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
    
        if(remoteHostStatus == NotReachable)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert Title"
                                                            message:@"Internet connection not currently available." 
                                                           delegate:nil
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:@"Ok", nil];
            [alert show];
            [alert release];
    
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
            return;
        }
    
        NSString *post = [NSString stringWithFormat:@""];
    
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding 
                              allowLossyConversion:NO]; 
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    
        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your Information Which You Want To Pass"]]];
        [request setHTTPMethod:@"POST"]; 
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
        [request setValue:@"application/x-www-form-urlencoded"  forHTTPHeaderField:@"Content-Type"]; 
        [request setHTTPBody:postData];
        urlData=[[NSMutableData alloc] init];
        [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
    

    重要方法下的 XML 解析

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [urlData setLength:0];
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [urlData appendData:data];
    }
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        xmlParser = [[NSXMLParser alloc] initWithData:urlData];
    
        [xmlParser setDelegate:self];
    
        [xmlParser parse];
    
        [urlData release];
    
    }
    
    #pragma mark -
    #pragma mark XML Parsing Delegate Methods
    
    - (void)parserDidStartDocument:(NSXMLParser *)parser
    {
    
    }
    
    -(void)parserDidEndDocument:(NSXMLParser *)parser
    {
         [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI 
     qualifiedName:(NSString *)qName
        attributes:(NSDictionary *)attributeDict
    {
    
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    { 
        NSCharacterSet *charsToTrim = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        string = [string stringByTrimmingCharactersInSet:charsToTrim];
    
        if(!currentElementValue)
            currentElementValue = [[NSMutableString alloc] initWithString:string];
        else
            [currentElementValue appendString:string];
        //NSLog(@"Current Element Value :- '%@'",currentElementValue);
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    {
        [currentElementValue release];
        currentElementValue = nil;
    }
    

    您也可以通过下线中止解析

    [xmlParser abortParsing];
    

    【讨论】:

      【解决方案2】:

      这个问题已经问过你至少应该自己尝试好, 在这里你去寻找完整的解决方案: to check internet connection

      希望能帮到你!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        • 2022-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-31
        • 1970-01-01
        相关资源
        最近更新 更多