简体   繁体   English

iPhone HTTP REST请求/响应

[英]iPhone HTTP REST Requests/Responses

I am very new to the iPhone SDK and have read many threads on RESTful service integration with the iPhone. 我是iPhone SDK的新手,并且已阅读有关与iPhone进行RESTful服务集成的许多主题。 However I am none the wiser. 但是我都不是明智的。 Can you help? 你能帮我吗?

I have some PHP and a MySQL database string on a server. 我在服务器上有一些PHP和MySQL数据库字符串。 I POST 3 pieces on information from the iPhone to the PHP which then does some thinking and chatting to the database. 我从iPhone到PHP发布3条信息,然后再进行一些思考并与数据库聊天。

It then returns 1, 2 or 3 values depending on the information POSTed and the database. 然后,它根据发布的信息和数据库返回1、2或3个值。

Where I am having the problem is capturing and storing the response in a way that can then be analysed on the iPhone. 我遇到的问题是捕获和存储响应,然后可以在iPhone上对其进行分析。

I have tried returning the data as a string (echo/return), an array, and finally XML. 我尝试过将数据作为字符串(回显/返回),数组和最后的XML返回。 None of which seems to work. 这些似乎都不起作用。

Does anyone have any thoughts on this, or might point me in the right direction? 有没有人对此有任何想法,或者可以为我指明正确的方向?

The ideal situation would be to end up with an array or 3 variables that I could test with a basic if statement. 理想的情况是最终得到一个数组或3个可以用基本if语句测试的变量。

Thanks in advance. 提前致谢。

Another alternative is to use the CocoaREST code available on github: http://github.com/sdegutis/CocoaREST . 另一种选择是使用github上提供的CocoaREST代码: http ://github.com/sdegutis/CocoaREST。 It was released only recently, but seems to be under active development and looks like its relatively straightforward to use. 它仅在最近才发布,但似乎正在积极开发中,看起来使用起来相对简单。

EDIT: As suggested in another answer, yet another approach would be to use an XML format. 编辑:正如在另一个答案中建议的那样,另一种方法是使用XML格式。 I would suggest a specific variant of that: you can use XML, but use plist-formatted XML. 我建议使用一个特定的变体:您可以使用XML,但可以使用plist格式的XML。 That way you can circumvent the need to parse the XML yourself and simply use something like: 这样一来,您就可以避免需要自己解析XML并只需使用类似以下内容的方法:

NSArray * myArray = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://example.com/xmlProducer"]];

(Similarly, you can create NSDictionary, NSString, etc from a URL) (类似地,您可以从URL创建NSDictionary,NSString等)

If you can send your data in JSON format, I recommend trying the Objective-C JSON framework . 如果您可以JSON格式发送数据,建议您尝试使用Objective-C JSON框架 I've found it very easy to get started with. 我发现入门非常容易。 JSON is very easy to work with for sending data in arrays and key-value dictionaries. JSON非常易于使用,用于以数组和键值字典的形式发送数据。 With the framework, you convert your json data to NSArrays or NSDictionaries with one method invocation. 使用该框架,您可以通过一种方法调用将json数据转换为NSArrays或NSDictionaries。

There are several nice tutorials on this blog . 该博客上有一些不错的教程。 Also, make sure to check out json.org . 另外,请确保签出json.org

Another alternative would be to have a look at the ObjectiveResource framework . 另一个选择是看看ObjectiveResource框架 It's designed to be used with Ruby on Rails based services, but if your API is truly RESTful, you should be able to use it. 它旨在与基于Ruby on Rails的服务一起使用,但是,如果您的API是真正的RESTful,则应该可以使用它。

将数据作为plist(这是XML的一个子集)发送回去,这是最容易解析的。

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

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