开发一个客户端 - 服务器的iPhone应用程序 [英] Developing a client-server iphone app

查看:153
本文介绍了开发一个客户端 - 服务器的iPhone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想开发一个客户端 - 服务器设计的iPhone应用程序(iPhone设备作为客户端和C#服务器),有两个问题:




  1. 是否有可能使用自己的笔记本电脑上运行的服务器?如果不是有哪些选择?

  2. 请我一定要发展自己的协议,用于传送消息?



  3. 所以,如果我理解发送诸如创建新的用户从客户端向服务器是如下消息的权利的过程:
    1.客户端将创建一个包含命令创建新用户,新用户的详细信息的JSON / XML。
    2.客户端将映射到服务器端的一个C#方法的URL通过发送HTTP请求这个JSON / XML(如HTTP请求的主体)。
    3。这会触发在服务器上的适当的方法和新的用户将在数据库上被创建。
    4.服务器将创建一个包含创造重播JSON / XML,并将其发送给客户端争夺HTTP响应(如HTTP响应的主体)。
    是正确的?


    解决方案

    您想XML或JSON通过HTTP。 Web服务和REST通过HTTP是为不同平台之间的互操作性问题产生这是你面对的是什么。



    由于您使用C#的服务器,你可以看看进入WCF和使用一个REST模式或SOAP(Web服务),以使您的操作和数据。关于数据,您可以通过电缆JSON或XML序列化的对象。



    有关iPhone的消费,我会建议REST(因为URL请求的路径基本上映射到C#方法)。从手机角度看,它只是一个URL请求,XML或JSON数据回来。



    在C#中您只需创建你的方法和装饰他们DataContract属性。然后,在你的方法,你将它们映射给url相对路径。搜寻网为WCF和REST服务。你可以在命令行窗口服务到IIS任何主机上运行。



    http://msdn.microsoft.com/en-us/library/bb412178.aspx



    在创建这些C#的服务,如REST,你可以打在浏览器中请求和看到的数据来通过。你也应该考虑Fiddler检查您的流量: http://www.fiddler2.com/fiddler2/



    在手机端,首先需要做的http请求。你可以做到这一点与iOS的类,但像ASIHTTPRequest包装使它更容易。一旦你获得了回响应,你要分析它。如果您选择XML iOS的类提供简单的方法来解析XML响应。如果你选择的JSON,有类,如SBJSON。



    HTTP ://allseeing-i.com/ASIHTTPRequest/ - (阅读 ASIHTTPRequest博客使用前)



    https://开头的github .COM /斯蒂格/ JSON的框架



    在iphone REST Web服务



    有也被称为RESTKit一个更高层次的框架,这使得iPhone的侧面容易得多。



    https://github.com/RestKit/RestKit < 。/ A>



    希望帮助绑在一起为你



    编辑:
    。加入了创建新的用户场景:




    客户端创建的数据(用户名,密码等用户对象.. ),并发送一个HTTP PUT请求的http:// yourserver /为MyService /用户。客户端序列化用户对象体内JSON / XML。




    什么是推荐/有效请求负载为一个REST PUT方法?



    PUT VS POST在REST




    服务器接收该请求。在服务器上,你有一个WCF则将MyService的服务(这是一个类)。它有一个公共用户CREATEUSER(用户用户)方法。在该方法中它创建做一个用户对象不管它需要做的(调用数据库等)。也许是因为服务器添加喜欢的用户ID信息,它返回用户对象。下面的文章有一个PUT请求的例子。




    http://damianm.com/tech/building-a-rest-client-with-wcf/




    客户端会得到像身份证等所有细节的响应和用户对象......会在身体JSON / XML。它会反序列化到手机上的用户对象



    服务器也可能使这样的话:
    /用户/ {ID} - >公众用户的getUser(字符串ID);



    If i want to develop an iPhone app with a client-server design (iPhone devices as the clients and a c# server),two questions:

    1. Is it possible to use my own laptop to run the server on? and if not what are my options?
    2. Do i have to develop my own protocol for transferring messages?

    So if i understood right the process of sending a message like "CREATE NEW USER" from the client to the server is as follow: 1. The client will create a JSON/XML containing the command "CREATE NEW USER" and the new user details. 2. The client will send this JSON/XML via HTTP request (as the body of the HTTP request) with a URL that maps to a c# method on the server side. 3. This will trigger the appropriate method on the server and the new user will be created on the database. 4. The server will create JSON/XML containing the replay "CREATED" and will send it to the client vie HTTP response (as the body of the HTTP response). Is that correct?

    解决方案

    You want either xml or json over http. Web Services and REST over http was created for interoperability problems between different platforms which is what you're facing.

    Since you're using C# for the server, you can look into WCF and use either a REST pattern or SOAP (web services) to expose your actions and data. Concerning the data you can serialize those objects over the wire as JSON or XML.

    For iPhone consumption, I would recommend REST (since that basically maps a url request path to a C# method). From the phones perspective, it's just a url request and xml or json data comes back.

    In C# you simply create your methods and decorate them with DataContract attributes. Then, on your methods you map them to url relative paths. Search the net for WCF and REST services. You can run it in any host from a command line to a windows service to IIS.

    http://msdn.microsoft.com/en-us/library/bb412178.aspx

    When creating those C# services, if REST, you can hit the requests in a browser and see the data come through. You should also look into Fiddler to inspect your traffic: http://www.fiddler2.com/fiddler2/

    On the phone side, you first need to make the http request. You can do that with iOS classes but wrappers like ASIHTTPRequest make it much easier. Once you get the response back, you have to parse it. If you choose XML the iOS classes offer simple ways to parse the xml response. If you choose JSON, there's classes like SBJSON.

    http://allseeing-i.com/ASIHTTPRequest/ - (Read this ASIHTTPRequest blog before using)

    https://github.com/stig/json-framework

    rest web services in iphone

    There's also a much higher level framework called RESTKit which makes the iPhone side much easier.

    https://github.com/RestKit/RestKit

    Hope that helps tie it together for you.

    EDIT: Adding the create new user scenario:

    The client creates a user object with the data (username, password etc...) and sends an HTTP PUT request to http://yourserver/myservice/users. The client serializes the user object to JSON/XML in the body.

    What is the recommended/effective request payload for a REST PUT method?

    PUT vs POST in REST

    The server receives the request. On the server, you have a WCF "myservice" service (it's a class). It has a "public User CreateUser(User user)" method. In that method it creates a user object by doing whatever it has to do (call database etc...). It returns the User object because perhaps the server added info like the user id. The article below has a put request example.

    http://damianm.com/tech/building-a-rest-client-with-wcf/

    The client would get the response and the user object with all the details like id, etc... would be in the body as JSON/XML. It would deserialize that into a User object on the phone.

    The server could also expose things like: /User/{id} --> public User GetUser(string id);

    这篇关于开发一个客户端 - 服务器的iPhone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆