How to implement and use google cloud sql in my an

2019-03-09 06:06发布

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.

2条回答
做个烂人
2楼-- · 2019-03-09 06:12

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"

查看更多
你好瞎i
3楼-- · 2019-03-09 06:28

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.

查看更多
登录 后发表回答