简体   繁体   中英

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 .

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

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

After editing gradle files, it suggests replacing "Tictactoe" in the code below with the name of your backend API

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. 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.

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

This name comes from several @Api annotations that are set when configuring the Endpoints API. It sounds like you probably started off with the appengine-java8/endpoints-v2-backend example, which has the following declaration:

@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 .

The important things to note here are both the name field, and the ownerDomain from the namespace . 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 . The Java class is just named after the @Api.name field (capitalized), so the class name is just Echo .

So, the fully qualified Java class you're looking for is:

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:

Let's say you set @Api.name to myApiName , and then set @Api.namespace.ownerDomain to mycompany.com . The fully qualified Java class that you would use from the generated client library is:

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. The much easier thing to do is just look at the client library generated by Endpoints.

When you run gradle endpointsClientLibs , the command outputs the location of the file that it has generated as a .zip file. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM