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.
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.
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With