1

I've looked around trying to find an answer to this question, but have so far been unsuccessful. I have a current version of an Android mobile application that stores items in the local database, and I'm trying to hook it up to a 'cloud' database (not sure if this is the right term) so that other people that download the app will be able to view and post things to the database.

I have an ec2 service set up, with my LAMP stack installed. I have the database set up on this server ready to go. How do I go about hooking up the Android application so that it can communicate (insert/view/delete items) with the database? I am a new developer so any help/insight/guidance you have is much appreciated!

  • You'll want to build a web service on the server for the app to talk to. Look for resources on how to make a web service using LAMP. – nasch Mar 05 '15 at 23:48
  • I actually wrote scripts in php that communicate between my app (via json) and the database. I'm able to insert/view entries just fine like this. Thanks! – ReturnOfTheMac Apr 01 '15 at 08:51

3 Answers3

0

You haven't mentioned the type of database you have setup on cloud. I am assuming it to be some kind of relational database (e.g. MySQL). You can use standard JDBC connection from a regular java code but it is not yet possible (supported) to do it directly from Android code.

I faced a similar issue, what I did was this:

  1. Create a Servlet and deployed it on GAE (Google App Engine).
  2. Make an HTTP request from your android application code to this Servlet (SELECT/UPDATE/DELETE).
  3. Initiate a JDBC connection to your database (on EC2) from that Servlet.
  4. Shoot the SQL statement to the database.
  5. Get the result and send the response back from servlet to your android code.

There may be other better solutions but I could not find any and tried this on my own, it worked like a charm, multiple times.

ThePatelGuy
  • 1,844
  • 1
  • 19
  • 18
  • Yes, it's a MySQL database. I actually wrote scripts in php that communicate between my app (via json) and the database. I'm able to insert/view entries just fine like this. Thanks! – ReturnOfTheMac Apr 01 '15 at 08:50
  • Happy to help! You can try the solution in the below answer – ThePatelGuy Apr 01 '15 at 08:53
0

After writing the previous answer, I kept on thinking of alternate solution.

FYI: This may or may NOT work !

  • Instead of hosting your servlet on GAE, you can host it on the same EC2 instance as your database, using tomcat or any other package.
  • This way your android code would communicate directly with the instance that has servlet and database deployed together.
  • In the servlet, you can shoot SQL queries internally and send the results back to android code.

How to host a servlet in Tomcat 7

Community
  • 1
  • 1
ThePatelGuy
  • 1,844
  • 1
  • 19
  • 18
0

Adding new security group and linking it with my instance..worked for me

Shubham
  • 434
  • 6
  • 12