[英]What is my backend API name? Where is it? Calling Google Cloud endpoint backend API from Android Studio Client
I'd like to use google cloud endpoint v2 to connect to App Engine (and eventually google cloud sql (NOT firebase as I'll be making complex queries)) in my android studio (v3.3) project . 我想在我的android studio(v3.3)项目中使用Google Cloud Endpoint v2连接到App Engine(并最终连接到Google Cloud sql(因为我将进行复杂的查询,请不要使用Firebase))。
The google cloud SDK, OpenAPI, Endpoints configuration is working (it can receive an echo "hello world" through windows PowerShell) and I built the client libraries following this tutorial: https://cloud.google.com/endpoints/docs/frameworks/java/gen_clients Google Cloud SDK,OpenAPI,Endpoints配置正在运行(它可以通过Windows PowerShell接收回显“ hello world”),并且我按照本教程构建了客户端库: https : //cloud.google.com/endpoints/docs/frameworks / JAVA / gen_clients
I've imported my client libraries into the Android Studio project file Libs and am currently following this tutorial to call backend APIs from Android Studio Client: https://cloud.google.com/endpoints/docs/frameworks/java/calling-from-android 我已将客户端库导入Android Studio项目文件Libs,目前正在按照本教程从Android Studio Client调用后端API: https : //cloud.google.com/endpoints/docs/frameworks/java/calling-from -Android
After editing gradle files, it suggests replacing "Tictactoe" in the code below with the name of your backend API 编辑gradle文件后,建议使用您的后端API的名称替换下面代码中的“ Tictactoe”
Tictactoe.Builder builder = new Tictactoe.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(), null);
service = builder.build();
I've spent 8 hours trying to figure what that would be. 我花了8个小时试图弄清楚那是什么。 I can't find any file in the imported libraries that works.
我在导入的库中找不到任何有效的文件。 I've tried using my Google Cloud project id on its own or followed by .appspot.com but nothing works.
我尝试单独使用我的Google Cloud项目ID,或者后面跟着.appspot.com,但没有任何效果。
I assume it must be somewhere in my Project File, I just need to know where. 我认为它必须在我的项目文件中的某个位置,我只需要知道位置。 And not knowing what it's called makes the search tricky.
而且不知道它的名字使搜索变得棘手。
Note: the Google Cloud site suggests asking technical questions on stackoverflow 注意:Google Cloud网站建议您就Stackoverflow提出技术问题
This name comes from several @Api
annotations that are set when configuring the Endpoints API. 此名称来自配置Endpoints API时设置的几个
@Api
批注。 It sounds like you probably started off with the appengine-java8/endpoints-v2-backend example, which has the following declaration: 听起来您可能是从appengine-java8 / endpoints-v2-backend示例开始的,该示例具有以下声明:
@Api(
name = "echo",
version = "v1",
namespace =
@ApiNamespace(
ownerDomain = "echo.example.com",
ownerName = "echo.example.com",
packagePath = ""
),
...
You can see the full definition in Echo.java . 您可以在Echo.java中看到完整的定义。
The important things to note here are both the name
field, and the ownerDomain
from the namespace
. 这里要注意的重要事项是
name
字段和namespace
的ownerDomain
。 Both of these together are used to generate the Java package that the code will belong in, but the segments of the ownerDomain
path are reversed (as is standard in Java packages, which represent the hierarchy in the opposite way of normal DNS segments), so the Java package ends up being com.example.echo.echo
. 这两个都用于生成代码将所属的Java包,但是
ownerDomain
路径的各段是相反的(如Java包中的标准,它以与正常DNS段相反的方式表示层次结构),因此Java包最终是com.example.echo.echo
。 The Java class is just named after the @Api.name
field (capitalized), so the class name is just Echo
. Java类仅以
@Api.name
字段命名(大写),因此该类名为Echo
。
So, the fully qualified Java class you're looking for is: 因此,您要查找的标准Java类是:
com.example.echo.echo.Echo
or, for your exact code snippet: 或者,对于您的确切代码段:
com.example.echo.echo.Echo.Builder
This might be confusing because this example uses the word "echo" so many times, it's hard to attribute where each individual echo
is coming from, but here's another example: 这可能会造成混淆,因为此示例使用了“ echo”一词很多次,很难归因于每个单独
echo
的来源,但这是另一个示例:
Let's say you set @Api.name
to myApiName
, and then set @Api.namespace.ownerDomain
to mycompany.com
. 假设您将
@Api.name
设置为myApiName
,然后将@Api.namespace.ownerDomain
设置为mycompany.com
。 The fully qualified Java class that you would use from the generated client library is: 您将从生成的客户端库中使用的标准Java类是:
com.mycompany.myApiName.MyApiName
However, I would not recommend that you try to manually figure out what the Java package and class name are based on these rules. 但是,我不建议您尝试根据这些规则手动确定Java包和类名。 The much easier thing to do is just look at the client library generated by Endpoints.
要做的更简单的事情就是查看由Endpoints生成的客户端库。
When you run gradle endpointsClientLibs
, the command outputs the location of the file that it has generated as a .zip
file. 当您运行
gradle endpointsClientLibs
,命令会将其生成的文件的位置输出为.zip
文件。 All you have to do is unzip this file and explore all of the generated code within it to easily see what the Java package structure looks like, and even the exact class definitions that Endpoints is generating for you. 您所需要做的就是解压缩该文件并浏览其中所有生成的代码,以轻松查看Java包结构的样子,甚至包括Endpoints为您生成的确切类定义。
If you're having trouble finding these classes in your IDE, I wonder if you might have accidentally missed steps 4-7 from the Generating a client library instructions: 如果您在IDE中找不到这些类时遇到麻烦,我想知道您是否可能意外地错过了生成客户端库指令中的步骤4-7:
4) Unzip the file using the unzip utility, or use another ZIP-compatible unarchiving utility:
unzip ./echo-v1-java.zip
5) Change directory: cd echo.
6) Build the client library:
gradle build
The output is in the `build/libs` directory. The exact filename depends on the version of the Java client. It is something like echo-v1-1.23.0-SNAPSHOT.jar.
7) Add the client library JAR to your Java or Android app.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.