Before explaining how to connect to a Web service using Cocoa, you have to understand what is Web Service.
A Web Service is a site that contains methods and functions that can be consumed by other applications or websites. And whukeeping my methods and functions in the web service when I can have them in my application or website?
Sometimes you need to maintain the methods and functions outside of our application, when you need several applications or sites, re-use the same methods and web service functions, and not replicated for each application.
There are two types of web services
SOA. - Are the services that use a base architecture of communication messages.
ROA. - Are the services that use resources (resources) and communication based architecture, which is an implementation of REST, using technologies like XML, JSON, XHTML, plists.
Now going to the point in question, let's see how you can connect to a type of Web Service REST ...
For HTTP you can perform the following orders:
1. GET
2. POST
3. PUT
4. DELETE
5. HEAD
In this case we will make a GET request to the service, so we need to follow the following steps:
- Know the web service information such as your address URI, or the methods that we check, and their respective parameters
HTTP-Request Ordering
-Obtain and crumble the result
The result that we will get will be something like this:
- <results>
- <head>
- <name>Nicolas</name>
- <entity>2</entity>
- <date>Tues Jun 07 06 GMT 2011</date>
- <description>Example of response</description>
- <method>ObtenerInformacion</method>
- <lon>-73.993525</lon>
- <title>iPhone Example</title>
- <url>http://iphoneexample:7074/.....</url>
- </head>
- </results>
Since we know that we're going to find once you make the request to the web service, let's move to write code:
- / / We set the web service URI (Remember that the @ is always strings below)
- NSStringEncoding encoding;
- NSError *error;
- NSURLResponse *response;
- NSError *error;
- //Handle the request
- NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
No hay comentarios:
Publicar un comentario