PDA

View Full Version : REST XML in iphone development




Sergio10
Aug 17, 2009, 10:41 AM
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



aikhan
Apr 17, 2010, 08:42 AM
to call some GET REST Service
- (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

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