简体   繁体   English

Android - Ruby on Rails - MySQL

[英]Android - Ruby on Rails - MySQL

I have started working on an Android app for which we need to use MySQL as database and Ruby on Rails for server side code. 我已经开始研究一个Android应用程序,我们需要使用MySQL作为数据库,使用Ruby on Rails作为服务器端代码。 We will be using SQLLite too on device(will sync both DB as and when required). 我们也将在设备上使用SQLLite(将在需要时同步DB)。 I searched the web and couldn't find any relevant tutorials/examples which can serve as a base to start with. 我搜索了网络,找不到任何相关的教程/示例,可以作为开始的基础。

I have gone through MySQL and ROR tutorials but still has confusion on connecting Android with ROR. 我已经完成了MySQL和ROR教程,但仍然对将Android与ROR连接起来感到困惑。

Can somebody share some relevant tutorials/code snippet which can explain the complete linkage of the technologies. 有人可以分享一些相关的教程/代码片段,可以解释技术的完整链接。 I mean how to send data from Android device to MySQL and vice versa. 我的意思是如何将数据从Android设备发送到MySQL,反之亦然。 I know the concept theoretically but not sure how and where to start with. 我从理论上了解这个概念,但不知道如何以及从哪里开始。

My sincere apologies for asking such a basic question or if I sound ambiguous but I am a beginner and need to complete this task. 我真诚地对提出这样一个基本问题表示歉意,或者我听起来含糊不清,但我是初学者,需要完成这项任务。 Thanks in Anticipation.. 在预期中感谢..

Here is a brief overview of what you should know to accomplish your goal. 以下是您应该了解的目标的简要概述。 I am not going to go that far into detail, especially since I have never personally used RoR. 我不会那么详细,特别是因为我从未亲自使用过RoR。 Note that some of these parts might not relate exactly to RoR, but the general idea behind it still applies. 请注意,其中一些部分可能与RoR无关,但其背后的一般概念仍然适用。 I will leave it up to you to research and figure out how to implement each individual component. 我将由您来研究并弄清楚如何实现每个单独的组件。

The general flow of everything is as follows: 一切的一般流程如下:

Android App <==> Network <==> Web Service <==> MySQL Android App <==>网络<==> Web服务<==> MySQL

Note the double-edged arrows since data will be flowing in both directions. 请注意双刃箭头,因为数据将在两个方向上流动。

The Android App is the client, and the Web Service and MySQL database are located on your Web Server . Android App是客户端, Web ServiceMySQL数据库位于Web Server I only included the Network part for completeness, but you shouldn't have to do anything once the data has been sent onto the network. 我只包含了网络部分的完整性,但是一旦将数据发送到网络,您就不必做任何事情。

A brief overview of each section: 每个部分的简要概述:

Android App: Android应用:

The Android App is the client that sends and retrieves data from the Web Server. Android App是从Web服务器发送和检索数据的客户端。 I am assuming that in your app you are going to allow the user to do some tasks which in essence becomes the data that you want to send to the server at some point. 我假设在您的应用程序中,您将允许用户执行某些任务,这些任务本质上会成为您希望在某个时刻发送到服务器的数据。

Take for example, the user should be able to enter his name and favorite animal. 例如,用户应该能够输入他的名字和喜爱的动物。 Lets say that there is an actual "Submit" button that the user may click. 假设用户可能会点击一个实际的“提交”按钮。 When this "Submit" button is clicked, it should wrap up the data into a proper format to be sent across the network. 单击此“提交”按钮时,它应将数据包装成适当的格式以通过网络发送。 Two of the most common ones are JSON and XML . 其中两个最常见的是JSONXML Once the data has been formatted properly, you will want to send the data to the server using some type of network protocol such as HTTP . 一旦数据格式正确,您将需要使用某种类型的网络协议(如HTTP将数据发送到服务器。 In order to send the data, you of course must have some URL as the target. 为了发送数据,您当然必须有一些URL作为目标。 Lets say the target is www.example.com/webservice.php . 让我们说目标是www.example.com/webservice.php This target is our Web Service located on the Web Server. 此目标是位于Web服务器上的Web服务。

Once you send the data, the server will respond with some data at which point you can do whatever you want with it. 一旦发送数据,服务器将回复一些数据,此时您可以随意执行任何操作。 Maybe display it to the user, or stick it in an SQLite database, or even both. 可能会将其显示给用户,或者将其粘贴到SQLite数据库中,甚至两者中。

The key thing to remember is that there is no magic going on. 要记住的关键是没有魔法。 Everything I have just described will be implemented in Java code that you will write in your Android Application at some point. 我刚刚描述的所有内容都将以Java代码实现,您将在某些时候在Android应用程序中编写。

Key Ideas you should research more and figure out how to implement in Java code: 关键想法你应该更多地研究并弄清楚如何在Java代码中实现:

  • JSON and XML JSON和XML
  • HTTP in Java Java中的HTTP
  • REST and SOAP REST和SOAP
  • Here is an excellent video on possible ways to set up the structure of your Android App. 是一个关于设置Android应用程序结构的可能方法的精彩视频。
  • Make sure that you are doing all network operations in your Android App on a different thread. 确保您在其他线程上的Android应用程序中执行所有网络操作。 An easy to use method is an Intent Service . 一种易于使用的方法是Intent Service

Web Service: 网络服务:

This is often the most confusing part. 这通常是最令人困惑的部分。 A Web Service is simply some entry point for clients attempting to access the Web Server . Web Service只是尝试访问Web Server客户端的一些入口点。 My explanation here might different slightly when using RoR , but the same idea applies. 在使用RoR ,我的解释可能略有不同,但同样的想法适用。 Notice above that the target URL was www.example.com/webservice.php . 请注意,目标URLwww.example.com/webservice.php The web service is literally the PHP code that exists on the Web Server, called webservice.php . Web服务实际上是Web服务器上存在的PHP代码,称为webservice.php In your Android App, when you send data to the target URL using HTTP , the Web Service code will be executed on the server (and also have access to the data that you sent to it). 在Android应用程序中,当您使用HTTP将数据发送到目标URL ,Web服务代码将在服务器上执行(并且还可以访问您发送给它的数据)。 Inside of your Web Service code, you will basically be extracting the data (which is in some format like JSON), grabbing the necessary parts, and then doing something with it. 在Web服务代码中,您基本上将提取数据(以某种格式,如JSON),获取必要的部分,然后使用它。 In this case you will most likely be querying the database. 在这种情况下,您很可能会查询数据库。 In PHP it is easy to write code that connects and queries a MySQL database that is also running on the server. 在PHP中,编写连接和查询也在服务器上运行的MySQL数据库的代码很容易。 When the response of the database is retrieved by the Web Server, you can send it back to the Android App. 当Web服务器检索到数据库的响应时,您可以将其发送回Android应用程序。 Just as before, remember, there is no magic going on. 和以前一样,请记住,没有任何魔法可言。 All of these ideas are implemented by writing some code. 所有这些想法都是通过编写一些代码来实现的。

Main ideas to research: 研究的主要思路:

  • Ruby on Rails web service Ruby on Rails Web服务
  • How to access a MySQL database using Ruby on Rails 如何使用Ruby on Rails访问MySQL数据库

MySQL Database: MySQL数据库:

This is where you will store the data on the Web Server. 您可以在此处将数据存储在Web服务器上。 I am not going to go that in depth here because this is just going to require you doing a lot of reading up on how to set up a MySQL database on a web server. 我不打算深入研究这一点,因为这只是要求您在如何在Web服务器上设置MySQL数据库时进行大量阅读。 It is also important that you learn how to create the appropriate queries such as SELECT , INSERT and so forth. 学习如何创建适当的查询(如SELECTINSERT等)也很重要。

Main Ideas to research: 研究的主要思路:

How to setup a MySQL database on a web server 如何在Web服务器上设置MySQL数据库

If you need any clarification, let me know! 如果您需要任何澄清,请告诉我们!

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

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