28

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.

Owehbeh
  • 579
  • 1
  • 5
  • 16
  • 2
    a month later and I still couldn't figure it out... I am not familiar with Servlets and Endpoint classes logic... I read a lot of tutorials and all are about web applications, but I am developing an android application and I just need the code to access my cloud sql database read and write from the app running on the phone... any one ? :( it's my senior project :( – Owehbeh Feb 24 '14 at 05:05

2 Answers2

12

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.

Gabriel
  • 624
  • 1
  • 7
  • 20
  • It looks like you didn't post the code in its entirety. Would you mind adding all of it? Thank you. – user2323030 Oct 13 '14 at 05:57
  • That is all you need. You will then run the query and do whatever you please with it. – Gabriel Dec 02 '14 at 03:03
  • I'm not familiar with Google Endpoints. However, after I figured out how to connect I found it easier to use the cloud datastore instead. It's cheaper and easier to set up. – Gabriel Jun 16 '15 at 21:21
  • Exactly where are you in Step 1? I don't see a menu like the one you're describing. –  Jun 24 '15 at 03:35
  • That's eclipse. Did you download the app engine SDK for eclipse and install it? – Gabriel Jun 25 '15 at 01:02
  • The DriverManager is creating problems. There is only Driver class with com.google.cloud.sql.jdbc... Driver manager is only available for java.sql. How did you get it to work? – cfl Jul 07 '15 at 08:02
  • Which step are you on? – Gabriel Jul 07 '15 at 13:36
6

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"

Romin
  • 8,708
  • 2
  • 24
  • 28
  • Thank you for your answer I went through this tutorial but I'm going to apply it maybe it will get clear after that. Again thanks :) – Owehbeh Jan 31 '14 at 06:01
  • 2
    I went through that tutorial but it doesn't say anything about accessing Cloud SQL. It only shows datastore... – Micro Jun 08 '15 at 16:48