简体   繁体   中英

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. We will be using SQLLite too on device(will sync both DB as and when required). 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.

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. 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. Note that some of these parts might not relate exactly to RoR, but the general idea behind it still applies. 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

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 . 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:

The Android App is the client that sends and retrieves data from the Web Server. 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 . 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 . In order to send the data, you of course must have some URL as the target. Lets say the target is www.example.com/webservice.php . This target is our Web Service located on the Web Server.

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.

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.

Key Ideas you should research more and figure out how to implement in Java code:

  • JSON and XML
  • HTTP in Java
  • REST and SOAP
  • Here is an excellent video on possible ways to set up the structure of your Android App.
  • Make sure that you are doing all network operations in your Android App on a different thread. An easy to use method is an 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 . My explanation here might different slightly when using RoR , but the same idea applies. Notice above that the target URL was www.example.com/webservice.php . The web service is literally the PHP code that exists on the Web Server, called 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). 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. 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. When the response of the database is retrieved by the Web Server, you can send it back to the Android App. 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
  • How to access a MySQL database using Ruby on Rails

MySQL Database:

This is where you will store the data on the Web Server. 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. It is also important that you learn how to create the appropriate queries such as SELECT , INSERT and so forth.

Main Ideas to research:

How to setup a MySQL database on a web server

If you need any clarification, let me know!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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