开发者

Send Request For Web Service And Important Delegate Methods For XML Parsing

I want to send request to service and in reply i am getting xml data.

开发者_运维百科So, please tell me how can i send request to the web-server with check of internet connection and also let me know the important delegate methods for xml parsing.


First You Also Need To get The Reachability.h and Reachability.m File

*You Can get Reachability File From here :-- *

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

Also Need To import .h file in your file in which you want to check internet connection

Define below variables in your .h file

NSMutableData *urlData;
NSMutableString *currentElementValue;

Also Define the Property of NSMutableString

Below is the code for 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];

For XML Parsing Below Important Methods

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

You Can also abort the parsing by Below Line

[xmlParser abortParsing];


this question have already asked you should try atleast first by yourself well, here u go for the complete solution: to check internet connection

hope will help u!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜