简体   繁体   中英

How can I connect iPhone and web service and get XML data?

I know how to connect to web server using an iPhone but now I have to connect the iPhone to a web service. I don't know how to do it and there is no demo or class available online.

Does anyone have any ideas?

You might find this tutorial, called Intro to SOAP Web Services useful. He shows how to package a request, send it to a web service, and read the response.

If you need some help with XML parsing, there is the TouchXML library which will give you a nice xml "document" to work with. Just be cautious of memory usage.

If you have to parse large XML message this tutorial about libxml and xmlreader in Cocoa will show you how to parse XML with the lower-level event-style parsers.

I've created an open source application for iPhone OS 3.0 that shows how to use REST & SOAP services in iPhone application, using XML (using 8 different iPhone libraries), SOAP, JSON (using SBJSON and TouchJSON), YAML, Protocol Buffers (Google serialization format) and even CSV from a PHP sample app (included in the project).

http://github.com/akosma/iPhoneWebServicesClient

The project is modular enough to support many other formats and libraries in the future.

The following presentation in SlideShare shows my findings in terms of performance, ease of implementation and payload characteristics:

http://www.slideshare.net/akosma/web-services-3439269

You can use these 2 lines which return the response of your HTTP request. You don't need any configuration. This code is usefull if you try to access a PHP scritp for example. After you just have to parse your result.

NSURL *URL=[[NSURL alloc] initWithString:stringForURL];
NSString *results = [[NSString alloc] initWithContentsOfURL :URL];

You can try this I made up some classes you can just include in your project and then follow the examples, you can even download a .net dataset and query it if you are using .net as the web service provider. http://grabalife.com/2009/10/19/using-net-web-services-and-dataset-objects-in-your-iphone-app/

In my opinion, you have two options :

  • Use a third party library. You can try wsdl2objc . It didn't work for me, but it is under active development so it improves every day.
  • Use a raw HTTP connection and handle every request/response. This is the way I followed. It is hard, so I'd also like to know a better approach.

You probably went with one of the solutions above, but this example app from Apple is great. It demonstrates how to use NSXMLParser to parse XML data on an RSS feed. SiesmicXML Example

devx上还有一个很好的例子 ,您可以通过http post和/或get连接

You can find good example here

How to consume webservice in Iphone

May it helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM