Build Android App using GCE
& GAE
By Love Sharma
Hello World
Some introductions are in order
● If you have question, raise a hand.
● There will be dedicated Q&A time at the end of the lecture
● All source code will be provided in the form of Github link.
Topics Today
● What is Google App engine?
● What are RESTful APIs?
● What is Google Cloud Endpoints?
● Testing API Locally / Google API Explorer
● Generate client libraries for Android / iOS
● Consuming endpoints in your android application
● Walkthrough sample tic-tac-toe project.
● Q&A
Google App Engine
● Platform as a Service (PaaS)
● Easy to build and maintain.
● Easy to scale as the traffic and storage needs grow.
● Supported programming languages
○ Java
○ Python
○ PHP
○ Go
● Datastore
● Easily integrated with Google Cloud SQL
When to use GAE?
● You don’t want to get troubled for setting up a server.
● You want instant for-free nearly infinite scalability support.
● You don’t feel like taking care of your own server monitoring tools.
● You need pricing that fits your actual usage and isn't time-slot based (App
engine provides pay-per-drink cost model).
● You are able to chunk long tasks into 60 second pieces.
● You are able to work without direct access to local file system.
RESTful APIs (quick recap)
● A web service, which uses HTTP as a transport.
● Central idea: there are resources which we want to export
● Resources are sent back and forth using their representation.
● REST = representational state transfer.
● Four main operations, mapped to HTTP method:
○ GET - read or list the data
○ POST - add new data
○ PUT - update existing data
○ DELETE - as follows from the name
What is Google Cloud Endpoints?
● Easily create API backend.
● Simply Annotate your code.
● Automatically generate client libraries.
● Leverage App engine on Mobile.
● Authenticate end users.
○ OAuth 2.0 Support
What is Google Cloud Endpoints?
Let’s see some `code`
Import messages libraries need to create
a backend API.
Then, the message classes are
defined. These messages are used to
define the requests and responses for
the API
Next, the sample defines the
API service. The API service
defines and implements the
methods that are available for
the API
Let’s see some `code`
This API has one methods:
● The board_get_movemethod return with a
single ‘O’ added to the board passed
in.
Finally, the sample defines the API server
which is responsible for routing requests to
an individual service.
APP = endpoints.api_server([TicTacToeApi])
Running and testing your backend API
In the project directory, start the development server:
~/path/to/python/sdk/google_appengine/dev_appserver.py .
When the backend is running successfully, a message similar to this one is
displayed:
INFO 2013-10-07 19:41:16,687 admin_server.py:117] Starting admin server at: http://localhost:
8000
In your browser, visit this URL:
http://localhost:8080/_ah/api/explorer
API Explorer
A. Click tictactoe to display the available methods.
B. Click tictactoe.board.getmove to display the
Explorer form for it.
C. Select state from Request body and enter current
state of tic-tac-toe board e.g., “X--------”
D. Execute in order to get next “O” move.
E. Notice the display of the request and response. In the
Response, you'll see the OK result and the returned
message, “XO-------”.
Generate Client Libraries
Invoke the Endpoints command line tool as follows:
google_appengine/endpointscfg.py get_client_lib java -bs desired_client_bundle your_module.
YourServiceClass
Using a more concrete example to generate the client library:
google_appengine/endpointscfg.py get_client_lib java -bs gradle tictactoe_api.TicTacToeApi
When successful, the tool displays a message similar to:
API client library written to ./tictactoe-v1.zip
Consuming endpoints in your android app
To add the client library to the Android project:
● Add a /libs directory to your project if it doesn't already have one. It should
be a peer to the /src directory.
● Copy the client library generated from the backend API into /libs.
● Select the library you just added, right-click, then select Add As Library to
your project.
Let’s integrate...
Creating the service object
In your project code, you must use a service
object to make requests to the backend API.
For unauthenticated requests, construct the
service object as follows:
Calling the backend API
In your project, call the API using the service
object.
In the snippet, we are requesting a list of all
Score objects on the server. If list required
parameters, or a request body, you would
provide them in the invocation. Android Studio
provides code completion to identity available
method calls, and their required parameters.
Let’s integrate...
Note: It is important to note that because
API calls result in requests over the
network, you are required to make
requests in their own thread. (This
requirement was added to recent versions
of Android, but it is a best practice even in
older versions.)
To do this, you use a Thread or
AsyncTask. For example:
Q & A
References
● https://cloud.google.com/endpoints/
● https://cloud.google.com/appengine/docs/python/endpoints/
● https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-
python
● https://console.developers.google.com
● https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-
android

Build Android App using GCE & GAE

  • 1.
    Build Android Appusing GCE & GAE By Love Sharma
  • 2.
    Hello World Some introductionsare in order ● If you have question, raise a hand. ● There will be dedicated Q&A time at the end of the lecture ● All source code will be provided in the form of Github link.
  • 3.
    Topics Today ● Whatis Google App engine? ● What are RESTful APIs? ● What is Google Cloud Endpoints? ● Testing API Locally / Google API Explorer ● Generate client libraries for Android / iOS ● Consuming endpoints in your android application ● Walkthrough sample tic-tac-toe project. ● Q&A
  • 4.
    Google App Engine ●Platform as a Service (PaaS) ● Easy to build and maintain. ● Easy to scale as the traffic and storage needs grow. ● Supported programming languages ○ Java ○ Python ○ PHP ○ Go ● Datastore ● Easily integrated with Google Cloud SQL
  • 5.
    When to useGAE? ● You don’t want to get troubled for setting up a server. ● You want instant for-free nearly infinite scalability support. ● You don’t feel like taking care of your own server monitoring tools. ● You need pricing that fits your actual usage and isn't time-slot based (App engine provides pay-per-drink cost model). ● You are able to chunk long tasks into 60 second pieces. ● You are able to work without direct access to local file system.
  • 6.
    RESTful APIs (quickrecap) ● A web service, which uses HTTP as a transport. ● Central idea: there are resources which we want to export ● Resources are sent back and forth using their representation. ● REST = representational state transfer. ● Four main operations, mapped to HTTP method: ○ GET - read or list the data ○ POST - add new data ○ PUT - update existing data ○ DELETE - as follows from the name
  • 7.
    What is GoogleCloud Endpoints? ● Easily create API backend. ● Simply Annotate your code. ● Automatically generate client libraries. ● Leverage App engine on Mobile. ● Authenticate end users. ○ OAuth 2.0 Support
  • 8.
    What is GoogleCloud Endpoints?
  • 9.
    Let’s see some`code` Import messages libraries need to create a backend API. Then, the message classes are defined. These messages are used to define the requests and responses for the API Next, the sample defines the API service. The API service defines and implements the methods that are available for the API
  • 10.
    Let’s see some`code` This API has one methods: ● The board_get_movemethod return with a single ‘O’ added to the board passed in. Finally, the sample defines the API server which is responsible for routing requests to an individual service. APP = endpoints.api_server([TicTacToeApi])
  • 11.
    Running and testingyour backend API In the project directory, start the development server: ~/path/to/python/sdk/google_appengine/dev_appserver.py . When the backend is running successfully, a message similar to this one is displayed: INFO 2013-10-07 19:41:16,687 admin_server.py:117] Starting admin server at: http://localhost: 8000 In your browser, visit this URL: http://localhost:8080/_ah/api/explorer
  • 12.
    API Explorer A. Clicktictactoe to display the available methods. B. Click tictactoe.board.getmove to display the Explorer form for it. C. Select state from Request body and enter current state of tic-tac-toe board e.g., “X--------” D. Execute in order to get next “O” move. E. Notice the display of the request and response. In the Response, you'll see the OK result and the returned message, “XO-------”.
  • 13.
    Generate Client Libraries Invokethe Endpoints command line tool as follows: google_appengine/endpointscfg.py get_client_lib java -bs desired_client_bundle your_module. YourServiceClass Using a more concrete example to generate the client library: google_appengine/endpointscfg.py get_client_lib java -bs gradle tictactoe_api.TicTacToeApi When successful, the tool displays a message similar to: API client library written to ./tictactoe-v1.zip
  • 14.
    Consuming endpoints inyour android app To add the client library to the Android project: ● Add a /libs directory to your project if it doesn't already have one. It should be a peer to the /src directory. ● Copy the client library generated from the backend API into /libs. ● Select the library you just added, right-click, then select Add As Library to your project.
  • 15.
    Let’s integrate... Creating theservice object In your project code, you must use a service object to make requests to the backend API. For unauthenticated requests, construct the service object as follows: Calling the backend API In your project, call the API using the service object. In the snippet, we are requesting a list of all Score objects on the server. If list required parameters, or a request body, you would provide them in the invocation. Android Studio provides code completion to identity available method calls, and their required parameters.
  • 16.
    Let’s integrate... Note: Itis important to note that because API calls result in requests over the network, you are required to make requests in their own thread. (This requirement was added to recent versions of Android, but it is a best practice even in older versions.) To do this, you use a Thread or AsyncTask. For example:
  • 17.
  • 18.
    References ● https://cloud.google.com/endpoints/ ● https://cloud.google.com/appengine/docs/python/endpoints/ ●https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe- python ● https://console.developers.google.com ● https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe- android