Sunday, April 3, 2011

Rest web service

I am aware of using the soap webservices on iphone .Now i want to use REST. Can anybody tell me, What are the methods associated with calling and use of rest webservice? Thanks in advance.

From stackoverflow
  • In many ways accessing RESTful web services from the iPhone is extremely similar to accessing SOAP web services. As your probably know, in a RESTful web service, you do not send an XML (or other data store) request.

    You want to look at the following classes:

    • NSURL
    • NSMutableURLRequest
    • NSURLConnection

    Here is some sample code for making the RESTful request:

    NSURL *url =[NSURL URLWithString:@"theURLofTheWebService"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    [theRequest setHTTPMethod:@"GET"];
    
    NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
    if(theConnection)
    {
     webData = [[NSMutableData data]retain];
    }
    else
    {
     NSLog(@"theConnection is NULL");
    }
    

    You also want to implement the following delegate methods of NSURLConnection:

    • - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    • - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    • - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    • - (void)connectionDidFinishLoading:(NSURLConnection *)connection

    Finally depending on the type of data returned (XML, JSON, etc.) you can use the appropriate ways to parse that data.

0 comments:

Post a Comment