我可以用什么来让Android应用程序与一个Rails应用程序进行通信 [英] What can I use to allow an android app to communicate with a rails app

查看:173
本文介绍了我可以用什么来让Android应用程序与一个Rails应用程序进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个应用程序,基本上可以让Android设备上的用户发送即时出现在Rails应用程序的消息。有没有人有建议,我会用什么来做到这一点?

I'm trying to create an application that basically lets a user on an android device send a message that instantly appears in the rails application. Does anyone have suggestions to what I would use to do this?

推荐答案

下面是一个快速演练。

让我们创建一个新的Rails应用程序

Let's create a new rails app

$ rails new simple-message
$ cd simple-message/

我们现在要创建一个名为消息的REST风格的资源,将管理信息从你的手机来了。

We are now going to create a RESTful resource called Message, that will manage the messages coming from your mobiles.

$ rails generate scaffold Message title:string content:text

创建该表将包含消息:

Create the table that will contain the messages:

$ rake db:migrate

启动boundled服务器和浏览器指向 http://0.0.0.0:3000/messages

从那里你可以的手动的创建新的消息,这将显示为一个列表。

From there you can manually create new messages, that will show up as a list.

对于每一个页面,您可以使用浏览器浏览有一个允许其他客户端编程调用相应的视图。

For every page you can navigate with your browser there is a corresponding view that accepts programmatic calls from other clients.

如果您浏览 http://0.0.0.0:3000/messages.xml 你会得到一个包含所有的XML你的消息。随着卷曲:

If you browse http://0.0.0.0:3000/messages.xml you will get an xml containing all you messages. With curl:

$ curl http://localhost:3000/messages.xml
<?xml version="1.0" encoding="UTF-8"?>
<messages type="array">
  <message>
    <created-at type="datetime">2010-11-27T18:24:36Z</created-at>
    <title>Just a message</title>
    <updated-at type="datetime">2010-11-27T18:24:36Z</updated-at>
    <id type="integer">1</id>
    <content>With some content</content>
  </message>
</messages>

它的时候添加新的消息:

Its time to add a new message:

$ curl -X POST -H 'Content-type: text/xml' -d '<message><title>Hello</title><content>How are you</content></message>' http://0.0.0.0:3000/messages.xml
<?xml version="1.0" encoding="UTF-8"?>
<message>
  <created-at type="datetime">2010-11-27T18:40:44Z</created-at>
  <title>Hello</title>
  <updated-at type="datetime">2010-11-27T18:40:44Z</updated-at>
  <id type="integer">2</id>
  <content>How are you</content>
</message>

您的Andr​​oid客户端将充当卷曲增加新的消息。

Your Android client will have to act as curl to add new messages.

现在,你需要一些认证,只允许特定的用户作为管理员,并restric使用您的API。在挖掘,以实现Rails的认证多种方式:

Now you need some authentication to allow only certain user to act as an admin and to restric the use of your API. Before digging for the various way to implement authentication in Rails:

  • 要发布的消息之前,您的用户进行身份验证?
  • 应在Web应用程序接受来自其他服务(如微博,Facebook,谷歌,OAuth的,OpenID的...),或者对你自己的用户数据库验证?
  • 是你的API开放给其他的客户端,或只是你的Andr​​oid客户端可以发帖?
  • 你需要知道的 <谁/ em>的公布消息(即用户登录)?
  • 你想阻止单个用户或应用程序发布消息到您的Web应用程序?

您可以找到不同策略的一个很好的回顾<一href="http://stackoverflow.com/questions/247110/looking-for-suggestions-for-building-a-secure-rest-api-within-ruby-on-rails/250308#250308">here.

You can find a nice recap of different strategies here.

这篇关于我可以用什么来让Android应用程序与一个Rails应用程序进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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