Android - Ruby on Rails - MySQL - android

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!

Related

Android auto refresh data from server

TLDR: I am wondering what is the best practice in android to do the following - when new data is stored in server side database inform and transfer data to android application ?
LONG STORY: I currently have server side application based on Node.js and MongoDB where I store my data in JSON format. Server (Node) exposes REST service which I call when I want add new data to database. I have another REST service which is called by android application client when refresh local data button is pressed. Everything is working like a charm, but I want to extend my android client application, so the data is transferred to application as soon as it comes to server. I have been looking at combination of:
AsyncTask(http://developer.android.com/reference/android/os/AsyncTask.html) and
BroadcastReciever (http://developer.android.com/reference/android/content/BroadcastReceiver.html).
But I am not sure if this is the right way to do it. Also I know I will have to make changes to my server side application, but this is not a problem.
So I am not looking for exact code how to do it, but for some advices what should I use.
http://en.wikipedia.org/wiki/Push_technology
http://en.wikipedia.org/wiki/Google_Cloud_Messaging
https://developers.google.com/cloud-messaging/
third one is the actual answer, but first ones have details

asp.net Web Api and mobile aplications

I just want to make a TV guide for Android and later some other Mobile Platform. I want to use an RSS updates to update my application. In this specific scenario I want to use http://tvprofil.net/ 's RSS resource. I thought that I could just call site's RSS XML from my Android app, parse it, show, and thats the end of the story.
But, my cousine told me that the best way to do things is through Asp.net Web Api Service. Something to deal with REST. So, If I have to change resource, to like some iptv provider's RSS I would just change the Service and not have to deal anything with my mobile application. Other advantage, he said, is if I want to build Windows Phone application or iOS aplication that works the same way, I would have that same service to do job for me and just create UI and basic things for those apps.
The problem is that I have no idea how Asp.net web Api is used.
Can anyone give me some usefull link, or even better, write an example of this thig. I just want the RSS from http://tvprofil.net/ to go through Asp.Net Web Api Service and that I can call service and get it as XML or ... even Json... or to get anything at all to my app.
The whole thing is pretty confusing to me
If a RSS feed has enough data for your application to function then you do not need ASP.NET Web Api. The only role ASP.NET Web Api could play in this scenario is if it acted as an intermediate. E.g. your client application contacts your server application (ASP.NET Web Api) which in turn pulls the data from the actual external data source (tvprofil.net RSS).
For completeness sake, a list of pro's and con's:
Advantages of using an intermediate server
Reliability. You can cache the data of the external data source and serve that data even while the external data source is offline. Also, by caching the data you can lighten the load on the external data source to a bare minimum.
Transformability. Your intermediate server can translate the data of an external data source to another format that is more suitable for your client application. This is useful when you have many external data sources that each serve data in another format. Your intermediate server acts as a layer of abstraction for your client application.
Disadvantages of using an intermediate server
More effort. It will take more effort to develop and host an intermediate server.
Reliability. You must ensure your intermediate server is online and connected to the internet 24/7

Safe connection between Android application and Apache server

Below I explained what I need and in the answer I would like to get information what technology, what kind of protocols, services etc should I use.
Also I know that there is a massive amount of information on the internet, but because there are so many choices I'm unable to make a decision. Here's what I want to have:
Android application which will sent and receive information from the internet/server
Of course server, which will do some rudimentary computation with the obtained information, then store it and at the end send it back to the client application
I have to admit that the subjects of networking, socketing, protocols, ciphering etc are the ones I've always run away from. Therefore advices such as what domains, databases etc utilize are highly welcome
Update: After a bit of a research I've implemented mechanism based on information from this site [1]. I already have a working MySql database with several tables inside. Also I've added php scripts to my Apache webspace and implemented all that's necessary within my Android app.
Now, as I understand communication between php's scripts and MySql database is safe (I've hardcoded the usr/pass within scripts). Therefore the only thing that has left to be done to secure the connection between my Android application and php scripts (I want to prevent the situation when everybody has an access to my php scripts). So my question should be pretty straightforward now, how can it be achieved?
[1] http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/
I think a simple TCP/IP connection via Sockets will do just fine for your purposes.
The lesson (with more information and some examples) from the Java-Docs can be found here.
The basic workflow is as follows:
Your Android-App opens a Socket-connection to your server on a
given port where a server-application listens.
The Android-App sends it's data (whatever that might be) to the
Server.
The Server reads the send data,
processes it,
stores it...
...then it sends back some response (maybe the computed values) to your
Android-App.
Your app can then figure out if everything went okay and use the
given data.
Answering my second question, all the php scripts has been put into directory with the password on my Apache server. To be able to trigger them I need to give this password which is hardcoded in my application. All the connection is done through HTTPS. Now everything works as planned!

Android client/server application?

I am supervising a project done by two students that involves retrieving information from a server and displaying it on an android phone. The students have never learnt networking, sql or java before (although they do know how to program) and are only now learning how to setup socketed connections between the phone and a sample server app that i gave them.
They will need to setup a simple sql database on the server on the campus network and be able to communicate with it and only pull information from the database and display it on the phone.
My current plan is that they will receive xml objects generated on the server side sent as a stream through the socket connection. They will then be able to generate a DOM using javax.xml classes and display it as they see fit on the phone itself.
Is this a valid method? What kind of problems can they expect to experience by following this technique? Is there another/better/correct way to do this (without using php or webservices)? The system will be for multiple users so will there be any significant performance issue with the proposed method?
Note 1: The phone never sends any request other than a single multicharacter identifier. The server interprets this identifier and returns information from preprogrammed queries and places it into an xml format.
Webservices sound like the correct approach for this, since you would not want to directly allow communication to the database over the internet.
The book "Unlocking Android" from Manning Publications, ISBN 978-1-933988-67-2 has a Chapter (6) dedicated to "Networking and web services".
Ah and then there is the one and only very nice video from a presentation regarding Android and RESTful webservices from the Google IO.
Couchbase, although from the NoSQL movement have a nice summary as well.
You could use JSON instead of XML, could be easier to parse and work with (feels more lightweight to me at least).
Sorry, forgot the answer for your search of problems:
Activity freezing upon freezing requests: Use additional threads for your requests
How to generally handle high latency
Handle offline behaviour

Needs workflow ideas for Android app

I'm planning on writing an android app that can view and update data on a local network Oracle DB.
I have already written a python script which checks the oracle db every couple of minutes and writes out XML files which I then plan to parse through my android app to display the data.
As I mentioned though this will only work if the android device is part of the same network (either connected locally or through a vpn), and the XML will be stored on a Unix server.
The question is how to access that Unix server with the android app? Can I use FTP via an android app, or should I be looking to change the python script to send the XML to an easily retrievable location?
EDIT To clarify what you mean, you have a web service running on something like tomcat, the client sends a request to get the data from the oracle db (can also use some form of authentication), the web service responds and sends the data in a format (XML, JSON etc.), the client then sends a request back to the web service to change some data on the oracle db, and in turn it does the clients bidding?
Normally you would create a web service to provide data to mobile clients. There are several reasons for this:
Web services are accessed via HTTP/HTTPS which is a standard protocol and is guaranteed to work on all networks. Corporate wifi networks are especially notorious for locking down protocols except for web and email.
Web services compared to static XML files have an advantage of creating response on the fly. Which means that data will be current.
Web services can take some input parameters and create response based on this parameters.
Authentication: it is a common approach to implement username/passord authentication in the web service, especially if you serve some private data.
Update:
REST is a very popular type of web service. Look at some implementations in Python: Recommendations of Python REST (web services) framework?
This book helped me a lot: http://oreilly.com/catalog/9780596529260
REST is a way of designing your web service. Folks much more intelligent than you and me have divined that all of the work needed for something to work on the net can be handled through a combination of HTTP status codes, HTTP verbs like POST, GET, DELETE, PUT etc. and a clear hierarchy of resources (nouns). It sounds very limiting but it's quite beautiful when it all works together.
Have a look at the Facebook Open Graph API (developers.facebook.com) to get an idea of what a proper REST service looks like.

Categories

Resources