简体   繁体   English

如何连接iPhone和Web服务并获取XML数据?

[英]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. 我知道如何使用iPhone连接到Web服务器,但现在我必须将iPhone连接到Web服务。 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. 您可能会发现这个名为Intro to SOAP Web Services的教程很有用。 He shows how to package a request, send it to a web service, and read the response. 他演示了如何打包请求,将其发送到Web服务以及阅读响应。

If you need some help with XML parsing, there is the TouchXML library which will give you a nice xml "document" to work with. 如果您需要一些XML解析方面的帮助,那么TouchXML库将为您提供一个很好的xml“文档”。 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. 如果你必须解析大型XML消息,那么关于Cocoa中的libxml和xmlreader的教程将向你展示如何用低级事件样式解析器解析XML。

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). 我已经为iPhone OS 3.0创建了一个开源应用程序,它展示了如何在iPhone应用程序中使用REST和SOAP服务,使用XML(使用8个不同的iPhone库),SOAP,JSON(使用SBJSON和TouchJSON),YAML,协议缓冲区(谷歌序列化格式)甚至来自PHP示例应用程序的CSV(包含在项目中)。

http://github.com/akosma/iPhoneWebServicesClient 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: SlideShare中的以下演示文稿显示了我在性能,易于实现和有效负载特性方面的发现:

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

You can use these 2 lines which return the response of your HTTP request. 您可以使用这两行返回HTTP请求的响应。 You don't need any configuration. 您不需要任何配置。 This code is usefull if you try to access a PHP scritp for example. 如果您尝试访问PHP scritp,此代码非常有用。 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. 您可以尝试这个我编写了一些可以包含在项目中的类,然后按照示例进行操作,如果您使用.net作为Web服务提供者,您甚至可以下载.net数据集并进行查询。 http://grabalife.com/2009/10/19/using-net-web-services-and-dataset-objects-in-your-iphone-app/ 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 . 你可以尝试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. 使用原始HTTP连接并处理每个请求/响应。 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. 您可能已经使用了上述解决方案之一,但Apple的这个示例应用程序非常棒。 It demonstrates how to use NSXMLParser to parse XML data on an RSS feed. 它演示了如何使用NSXMLParser来解析RSS提要上的XML数据。 SiesmicXML Example SiesmicXML示例

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

You can find good example here 你可以在这里找到好的例子

How to consume webservice in Iphone 如何在Iphone中使用webservice

May it helps you 可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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