Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement and use google cloud sql in my android app (eclipse)

this would look like a dumb question and it may look like I didn't search out there for an answer but.

The problem is that I am developing an android app and at a certain point I new about

Google Cloud SQL

and

Google App Engine

so I watched like 20-30 tutorial and started implementing, but now I'm stuck and can find no tutorial that shows a step by step simple android code.

Here's what I've done and where I'm stuck right now:

-my android app is working great no single error

-created an app engine project

-turned on Google Cloud SQL service and it's API service and paid for that

-created an instance in the cloud

-and "through the api console" created a table and a small database in my instance

-generated an App Engine backed for my application package

And here it's where I got stuck !!! I don't know how to use the generated files, how things work, how can I access the table in the cloud through my app, COULD FIND NO TUTORIAL explaining how does that happen, all tutorials out there just skip that step as if it's the easiest thing in the world.

I just want to know how does things work together? where to right the methods, what do I have to do to get my data from the table in the instance which is in the cloud...

I would appreciate even any link :) thank you.

like image 409
Owehbeh Avatar asked Jan 30 '14 04:01

Owehbeh


People also ask

How do I connect Google Cloud to SQL?

In the Google Cloud console, go to the Cloud SQL Instances page. To open the Overview page of an instance, click the instance name. Select Connections from the SQL navigation menu. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed.

How do I deploy Android apps to Google Cloud?

In Android Studio, open an existing Android application that you want to modify, or create a new one. Select the Android app module under the Project node. Then click Tools > Google Cloud Endpoints > Create App Engine Backend. In the wizard, enter the Project ID, Project Number, and API Key of your Cloud project.


2 Answers

If you haven't got this figured out yet, this is what I did.

In the developer console note your project number and your API key. Also make sure your cloud instance allows access to your project ID.

  1. Create an app engine connected project. File > New > Other > Android > App Engine Connected Android Project.
  2. Enter your project number and API key.
  3. Once you create the project, then right click the generated app engine project > Google > App Engine Setting and enter your project ID from the developer's console in the Application ID field.
  4. Right click the generate app engine project > Google > Generate Cloud Endpoint Client Library
  5. Right click the generate app engine project > Google > Deploy to App Engine

Now you can call you cloud SQL database from the android app. but you have to do it as an AsyncTask. It did not work for me until I did this. Create an AsyncTask class and in the doInBackground connect to your DB. I did mine like this:

  public class Connect extends AsyncTask<Context, Integer, Long> {      protected Long doInBackground(Context... contexts) {          Connection connection;         String query = "Some query";         try {             Class.forName("com.mysql.jdbc.Driver");             connection = DriverManager.getConnection("jdbc:mysql://<your cloud IP address>/<database schema you want to connect to>", "<user>", "<password>");              Statement statement = connection.createStatement();             ResultSet resultSet = statement.executeQuery(query); 

If you already created an android project, just right click that project > Google > Generate App Engine Backend and start from step 2. I hope this helps.

like image 142
Gabriel Avatar answered Sep 20 '22 23:09

Gabriel


You are almost there. The recommended mechanism for you would be to expose your App Engine hosted functionality via a REST service and invoke those services from your Android application.

Google makes it easier for you to do that via the Cloud Endpoints functionality. This will help generate an Endpoints Service (think REST Service) for your Mobile Backend. It will also generate a set of Client classes (in .java for your Android application) that you can use easily to invoke the services from your Android client.

Check out this in-depth tutorial that covers "How to build a mobile app with an App Engine backend"

like image 24
Romin Avatar answered Sep 22 '22 23:09

Romin