objective-c 如何连接 iPhone 和 Web 服务并获取 XML 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/352676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I connect iPhone and web service and get XML data?
提问by
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.
我知道如何使用 iPhone 连接到网络服务器,但现在我必须将 iPhone 连接到网络服务。我不知道该怎么做,也没有在线演示或课程。
Does anyone have any ideas?
有没有人有任何想法?
回答by amrox
You might find this tutorial, called Intro to SOAP Web Servicesuseful. He shows how to package a request, send it to a web service, and read the response.
您可能会发现这个名为 Intro to SOAP Web Services 的教程很有用。他展示了如何打包请求、将其发送到 Web 服务并读取响应。
If you need some help with XML parsing, there is the TouchXMLlibrary which will give you a nice xml "document" to work with. Just be cautious of memory usage.
如果您需要一些 XML 解析方面的帮助,可以使用TouchXML库,它可以为您提供一个不错的 xml“文档”。请注意内存使用情况。
If you have to parse large XML message this tutorial about libxml and xmlreader in Cocoawill show you how to parse XML with the lower-level event-style parsers.
如果您必须解析大型 XML 消息,本教程关于 Cocoa 中的 libxml 和 xmlreader将向您展示如何使用较低级别的事件样式解析器解析 XML。
回答by Adrian Kosmaczewski
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、协议缓冲区( Google 序列化格式)甚至来自 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 中的以下演示文稿显示了我在性能、易于实施和有效负载特性方面的发现:
回答by Geraud.ch
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.
您可以使用这 2 行来返回 HTTP 请求的响应。您不需要任何配置。例如,如果您尝试访问 PHP 脚本,此代码很有用。在你只需要解析你的结果之后。
NSURL *URL=[[NSURL alloc] initWithString:stringForURL];
NSString *results = [[NSString alloc] initWithContentsOfURL :URL];
回答by Guido
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.
- 使用第三方库。你可以试试wsdl2objc。它对我不起作用,但它正在积极开发中,因此每天都在改进。
- 使用原始 HTTP 连接并处理每个请求/响应。这是我遵循的方式。这很难,所以我也想知道更好的方法。

