[WHOOSH]
PAUL RASHIDI: Hi.
My name is Paul Rashidi and I'm with the Google Cloud Platform
team.
Today we're going to discuss how to build the Hello Endpoints
Android application from scratch.
We'll be helped by GitHub gist that
will contain most of the code we would normally
write and commented in as we talk about it.
So let's get started.
The first step we'll take is setting up a Cloud Endpoints
back end.
This is actually pretty easy.
We cloned a Cloud Endpoints back end sample from GitHub
like this, but do note there is also
a Python version available as well.
In order to get the Java back end running on a local machine,
we'll perform two steps.
We'll uncomment a line in this Maven pom XML file
to specify that we want to run on all IP addresses
on the local machine.
This will allow our Android application
to access the back end application,
since will be using an emulator, which can't access local host.
The other change we'll need to make
is editing the App Engine Web XML file
to update the app's [? vod ID ?].
Since we won't be deploying remotely to App Engine,
this idea is only affecting the Java package
name of our generated classes.
You should use the developer console to register an ID
and use that value here.
Easy enough.
We'll save and exit.
So, just to quickly recap-- we only made these two changes.
Now, to run this back end locally,
we will invoke it via Maven.
In this command, install is building our application.
And the dev server command is running it
on our local dev server.
You should know, the first time you
run this command on a machine, it will take a little bit
longer than normal because Maven will download the App Engine
plugin.
Now that our server is running, let's
take a look at it in our API Explorer.
This is very handy if you're not familiar with an API
but still want to play around with it.
We'll use this URL.
We'll select our API.
And we'll run the list command here.
We see that our service is returning two messages-- hello
world and goodbye world.
Now that we have a working test server,
let's get to the Android app.
We'll switch to Android Studio to create our project.
The first thing we're going to do
is create a new Android application via the New Project
Wizard.
We'll adjust some project names here and then select
a minimum API level of eight.
The client libraries that we will create
will need this minimum level.
We'll take the rest of the defaults
and create this project.
I'll speed the video up a little so you
don't have to wait for the initial build.
And there we have it.
This is a typical default Android project
with a single main activity.
We'll just close a few windows and put focus
on our GitHub gist.
This gist contains all the code that we
are writing during this tutorial so you
don't have to watch me type.
You can access this gist at the URL on the screen.
Let's just briefly look at the files
before we copy them into our project.
First we have an application.java,
which we'll use as a way to store messages retrieved
from our back end API.
You will want to use content provider instead
of a global variable like this.
But we're doing this for sample code simplicity.
The next file is the main activity file.
All the code pertaining to connecting to the back end API
is commented out.
The code that is not commented out
implements a simple list adapter that
will let us display messages on the screen retrieved
from the back end API.
The next XML file defines the GUI for the app.
We'll preview this in a minute.
This file contains a snippet of Gradle
dependencies we'll use to compliment our client library
classes.
This final file is a simple move script
that will move these files into the right locations
in our sample project.
So now we'll flip over to a new terminal window
and download this gist.
We'll also enable permissions and execute our move script.
And now the files we reviewed are in our project.
We'll first take a look at the main activity
code we glossed over before.
You can see the import statements
for the client library are throwing an error.
That's because we need to generate our client library.
Let's do that now.
To do this, we'll flip back to our terminal window.
We'll stop our back end API server.
And we'll run this endpoints_get_client_lib goal
via Maven.
This will generate the client library.
Next we'll move the zip file continuing the generated Java
classes to our current directory.
Then we'll unzip it and move it to our Android product source
directory.
Then we'll restart our local back end API server.
We'll now switch back to Android Studio
and reference this Gradle build file dependency snippet.
We're going to copy this and paste it into the build Gradle
file for our project.
This will put all the dependencies
that the generated client library
code needs into our project so that we can build and run.
We'll now save and force a rebuild of the project.
If you still have some unresolved imports,
you likely need to run the sync project with Gradle function
here.
Next, we're going to look at the GUI layout that we
that we received from our gist.
It's a simple layout with a simple button
for our back end for each of the API methods.
First, we can get a message by ID.
You'll notice we use the onclick attribute to dynamically invoke
methods in our code.
Next, we have a button to list all the greetings
on the server.
Here we have a send greeting button,
which maps to the multiply message on the back end.
Finally, in this UI, you'll notice a list view
at the bottom, which we'll use to display our messages.
You probably also noticed the Sign
In button and the Authenticated Message buttons.
We won't talk about those in this video.
But full details are in the read me
as a full sample code I referenced
at the beginning of the video.
We'll now close this and go back to our main activity code.
The first code we're going to comment in
is the onclick list greeting method.
This method is invoked when the List Greetings
button is clicked on the GUI we just looked at.
You'll see we're using an anonymously defined AsyncTask
to make our back end API calls.
On that background thread, we are first
getting a handle to our back end API via this utility method.
We'll then create a list greeting command object.
And we immediately invoke it to retrieve a greeting.
We pass the retrieve greeting back from the method.
And then it's passed back to us in the onPostExecute method
right here.
In this method, we'll use the Display Greetings
utility to display it in our UI.
This line of code then kicks off this anonymously
defined AsyncTask without any parameters.
We'll next uncomment onclick get greeting
to implement get greeting button functionality.
This looks the same as before, except we're
getting the greeting ID parameter from the UI here.
We format it for use in our API call.
And then we use the AsyncTask here again.
Finally, we'll comment in the onclick
send greetings method to implement the last button.
The only real difference here is that we're
pulling two parameters from the UI instead of one, as before.
Now, we'll take a look at some of the utility
code we used above.
This first method is the one we used
to get a handle to our back end API.
We're constructing a builder object
that was supplied by the generated API classes.
We are using two class variables here to construct our builder.
Let's comment those in above.
As you can see, we're commenting in Android compatible JSON
factory and an HTTP transport.
Let's scroll back down.
In this builder, we have the capability
of providing a different root URI.
This is a handy way of redirecting your client library
to point to your local development or testing servers.
We're going to do that right now with my IP address.
This is so any API calls we make will
go against my local development server
from the Android emulator.
The final code we'll comment in here
is the Display Greetings method.
This method simply updates our list adapter
to display the greetings passed to it.
Again, you typically want to use content providers
to store and provide data to list adapters.
But we are simplifying this sample code.
The code down here is the list adapter
we saw earlier in the GitHub gist, nothing new.
We're close to being finished, I promise.
Next, we'll add internet permissions
to our Android manifest file.
We'll adjust our UI style to be dark instead of the light one.
This will make our content a little bit more
visible when we run it in the emulator.
We're about done.
Let's go ahead and start an emulator.
We click here and create an emulator.
Any will do unless you've added authentication to your API.
And in that case, you would need to select a Google APIs
emulator.
We'll start this emulator.
And I'll scale it so that it fits on the screen.
Remember to utilize the accelerator extension
in the SDK manager if it is available for your development
platform.
OK, now that we're up and running with the emulator,
let's make one last change.
We need to define our custom application
class in our Android manifest.
I'll do that really quick here.
And then we'll deploy to our emulator.
Whoo.
It's running.
Alright.
The first thing we'll test is list greeting.
We click the button.
And we can see hello and goodbye world messages.
These are the same messages we saw
when we used the API explorer before.
Now, we'll test get greeting by putting in an index of zero.
And it returns the correct hello world
greeting from the back end API.
The ID of 1 returns the goodbye greeting correctly as well.
Then we'll test send greetings by putting in a text message
and specify that it needs to be repeated three times.
And the back end API successfully
returned our message here as well.
Well, that's about it.
Our tests are working.
And our new Android application is communicating
with an API we're running on our local server.
I want to thank you all for your time.
The Google Cloud Platform Team looks forward
to seeing what you build with this.
And we look forward to your feedback as well.
Thanks.
[WHOOSH]