REST XML in iphone development

Discussion in 'iOS Programming' started by Sergio10, Aug 17, 2009.

  1. Sergio10 macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,

    How to implement REST XML?

    What does it mean in general? As I understand application sends request as http. And receives XML as respons.
    Correct?

    Thank you
     
  2. aikhan macrumors newbie

    aikhan

    Joined:
    Feb 8, 2010
    #2
    to call some GET REST Service
    Code:
    - (void)getPickUpLinesFromWebService{
    	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    	NSString *URLstr = GET_PICKUP_LINES_URL;
    	
    	NSURL *theURL = [NSURL URLWithString:URLstr];
    	NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    	
    	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    	
    	if(theConnection){
    		responseData = [[NSMutableData data] retain];
    	}
    	else{
    		[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    	}
    
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    	[responseData appendData:data];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    	[self parsePickUpLines:responseData];
    	[connection release];
    	[responseData release];
    }
    
    - (void)parsePickUpLines:(NSData *)pickUpLinesXMLData{
    	if(pickUpLinesParser)
    		[pickUpLinesParser release];
    	pickUpLinesParser = [[NSXMLParser alloc] initWithData:pickUpLinesXMLData];
    	[pickUpLinesParser setDelegate:self];
    	[pickUpLinesParser setShouldResolveExternalEntities:NO];
    	[pickUpLinesParser parse];
    }
    
    And the you implement NSXMLParser delegate Methods

    For POST REST Service
    Code:
    - (void)SomeServiceMethod
    {
    	NSData *thumbsupData = [[NSString stringWithFormat:
    								 @"<rootElement> \
    								<element> \
    								<line_id>%@</line_id> \
    								<thumbsup>%@</thumbsup> \
    								</element> \
    								 </rootElement>",  line_id]
    								dataUsingEncoding: NSASCIIStringEncoding];
    	
    
    	
    	NSString *URLstr = SOME_URL;
    	NSURL *theURL = [NSURL URLWithString:URLstr];
    	
    	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
    	
    	[theRequest setHTTPMethod:@"POST"];
    	[theRequest setValue:@"text/xml"forHTTPHeaderField:@"Content-type"];
    	[theRequest setHTTPBody:thumbsupData];
    	
    	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    	
    	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    	if(!theConnection){
    		NSLog(@"Error, Invalid Request");
    		
    	}
    }
    Its pretty easy just follow the code youll understand
     

Share This Page