What can I use to allow an android app to communicate with a rails app

后端 未结 3 1809
-上瘾入骨i
-上瘾入骨i 2020-12-19 14:53

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 suggest

相关标签:
3条回答
  • 2020-12-19 15:27

    The best way to do this is by creating an API for your rails app. Then you can use HTTP post request from your Android Application.

    0 讨论(0)
  • 2020-12-19 15:37

    Here is a fast walkthrough.

    Preparing the base application

    Let's create a new rails app

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

    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
    

    Start the boundled server and point your browser to http://0.0.0.0:3000/messages

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

    Your API is already there

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

    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>
    

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

    Securing your web app

    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:

    • should your user authenticate before posting a message?
    • should the web app accept authentication from other services (i.e. Twitter, Facebook, Google, OAuth, OpenID...) or against your own user database?
    • are your API open to other clients, or just your Android client can post messages?
    • do you need to know who posted the message (i.e. the user login)?
    • do you want to block a single user or an application to post messages to your web app?

    You can find a nice recap of different strategies here.

    0 讨论(0)
  • 2020-12-19 15:50

    If you respect the REST paradigms it's as easy as a POST to an URL.

    0 讨论(0)
提交回复
热议问题