如何在我的 android 应用程序 (eclipse) 中实现和使用 google cloud sql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21447876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement and use google cloud sql in my android app (eclipse)
提问by Owehbeh
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
问题是我正在开发一个 android 应用程序,并且在某个时候我对
Google Cloud SQL
谷歌云 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.
所以我看了 20-30 教程并开始实施,但现在我卡住了,找不到任何教程可以显示一步一步的简单 android 代码。
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
- 启用 Google Cloud SQL 服务及其 API 服务并为此付费
-created an instance in the cloud
- 在云端创建了一个实例
-and "through the api console" created a table and a small database in my instance
- 并且“通过 api 控制台”在我的实例中创建了一个表和一个小型数据库
-generated an App Engine backed for my application package
- 为我的应用程序包生成了一个 App Engine
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.
我什至会感谢任何链接:) 谢谢。
采纳答案by Gabriel
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.
在开发者控制台中,记下您的项目编号和 API 密钥。还要确保您的云实例允许访问您的项目 ID。
- Create an app engine connected project. File > New > Other > Android > App Engine Connected Android Project.
- Enter your project number and API key.
- 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.
- Right click the generate app engine project > Google > Generate Cloud Endpoint Client Library
- Right click the generate app engine project > Google > Deploy to App Engine
- 创建一个应用引擎连接的项目。文件 > 新建 > 其他 > Android > App Engine Connected Android 项目。
- 输入您的项目编号和 API 密钥。
- 创建项目后,右键单击生成的应用引擎项目 > Google > App Engine Setting,然后在应用程序 ID 字段中从开发人员控制台输入您的项目 ID。
- 右键单击生成应用引擎项目> Google > 生成云端点客户端库
- 右键单击生成应用引擎项目 > Google > 部署到 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:
现在,您可以从 Android 应用程序调用云 SQL 数据库。但您必须将其作为 AsyncTask 来执行。在我这样做之前,它对我不起作用。创建一个 AsyncTask 类并在 doInBackground 中连接到您的数据库。我是这样做的:
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.
如果您已经创建了一个 android 项目,只需右键单击该项目 > Google > Generate App Engine Backend 并从第 2 步开始。我希望这会有所帮助。
回答by Romin
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.
你快到了。为您推荐的机制是通过 REST 服务公开您的 App Engine 托管功能,并从您的 Android 应用程序调用这些服务。
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.
Google 通过 Cloud Endpoints 功能让您可以更轻松地做到这一点。这将有助于为您的移动后端生成端点服务(想想 REST 服务)。它还将生成一组客户端类(在 .java 中用于您的 Android 应用程序),您可以轻松使用这些类从您的 Android 客户端调用服务。
Check out this in-depth tutorial that covers "How to build a mobile app with an App Engine backend"
看看这个深入的教程,涵盖“如何建立一个移动应用与App Engine的后端”