我可以用什么来让Android应用程序与一个Rails应用程序进行通信应用程序、可以用、通信、Android

2023-09-13 01:40:12 作者:春风暖人心

我试图创建一个应用程序,基本上可以让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客户端可以发帖? 你需要知道的 的公布消息(即用户登录)? 你想阻止单个用户或应用程序发布消息到您的Web应用程序?

您可以找到不同策略的一个很好的回顾here.

You can find a nice recap of different strategies here.

 
精彩推荐
图片推荐