How to call Web service

Hi,

I would like to consume a web service by passing some parameters to that.

Let's say that there is a web service which accepts two numeric parameters and returns the multiplication of these numbers.

Using iPhone SDK can I call this web service by passing some parameters and get the result.

Any example to do the same will be a great help.

Regards,
Punit

Mac OS X (10.5.3)

Posted on Jul 11, 2008 5:30 AM

Reply
11 replies

Jul 14, 2008 2:04 AM in response to punit jain

Yes, but if you're using a protocol like SOAP or XML-RPC, you will have to generate and interpret the XML being exchanged yourself. (You can use NSXMLParser to parse the XML, but your code will still have to actually make sense of it.)

Needless to say, this is a big pain, because SOAP and XML-RPC are ridiculously complicated for what they're trying to do. If you're able to change how the web service is implemented, consider using a simpler, home-grown protocol based on CGI, or perhaps a RESTful web application. Even if you can't change the web service, consider using your server to translate between the web service's protocol and a simpler one for the device; you'll save more time by using your server's web service libraries than you'll lose writing the translator.

Jul 14, 2008 6:23 AM in response to punit jain

If you do an device spesific xml you can do the following to initiate the NSXMLParser:

NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:urlInstance];

Then check the documentation on NSXMLParser to see what delegate to implement.

You also have a good tutorial on how to parse xml in the seismic example url from apple:
http://developer.apple.com/iphone/library/samplecode/SeismicXML/index.html

Enjoy

Jul 14, 2008 6:27 AM in response to punit jain

Go grab TouchXML( http://code.google.com/p/touchcode/w/list)...then use something like

NSURL *url = [NSURL URLWithString:@"http://www.yourdomain.com?num1=2&num2=4"];
NSString *xmlSource = [NSString stringWithContentsOfURL:url];
NSError* err;

CXMLDocument *theXMLDocument = [[[CXMLDocument alloc] initWithXMLString:xmlSource options:0 error:&err] autorelease];

You will then have the XML as a browsable tree...they give an example in the code how to use it.

Jul 15, 2008 2:38 AM in response to punit jain

Again: Yes, but it will be a lot of work, because there are no libraries to make it as "simple" as it would be in, say, the .Net Framework. (In .Net, a lot of code goes into making web services look simple. The iPhone SDK team decided not to include similar code on the iPhone, even though it had already been created for Mac OS X; if I had to guess, I'd say it's either too slow or too large for the device.)

You will find that interfacing with this kind of web service is not easy, because you will have to create and interpret all the XML in your own code, rather than using the kind of built-in libraries you are used to. If at all possible, I recommend redesigning the web service or using a server to translate between SOAP and a simpler protocol.

However, if you have to use this web service in this way, it is possible. You will have to read the SOAP and WSDL standards and implement enough of them to call your web service. There is no sample code available for using SOAP on the iPhone, because this is very difficult. What you will be able to find is samples of sending HTTP requests which return XML and then parsing that XML. The SeismicXML sample is probably the best sample for you to study.

Good luck.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to call Web service

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.