Incorporating Google cloud in Android app

Google cloud Google Cloud Endpoints will help you create an outstanding mobile application, since these are designed to facilitate developers to create a shared web backend by developing and hosting APIs conveniently. Let’s have a look at various sections below, to understand how it is used for accomplishing various tasks. # Bird’s-eye view over Google Cloud Endpoints The amateur or newbie app developers, who are not familiar with the term Google Cloud Endpoints, here is a brief description. Google developers have put in brilliant efforts to ease the developer’s life by offering them a great service for creating custom APIs that will facilitate your client application’s interaction. It is highly optimized and provides server-client interactions and protocols; it thus, save you from a heck lot of coding. The biggest incentive for this service is that you can custom create the APIs, based on your desirable specifications and sensibilities. You also get a free rein in the sense that you can create APIs of the likes of REST and RPC. Moreover, it offers supple development by allowing one to instantly implement changes as often as one likes to. # How to create Endpoints on Eclipse For creating a project and creating Endpoints on Eclipse, follow the below mentioned simple steps.
  • Before beginning the process, it is assumed that your device is equipped with certain requisite softwares and tools including, App Engine SDK, Android SDK, Eclipse and Google plugin (to support your Eclipse).
  • You will need an Android client application, so create that in Eclipse.
  • For creating the App Engine:
    • Right click on your Android application.
    • Navigate to Google and then select an option “Create App Engine Backend”.
  • For creating basic Endpoint classes:
    • Right click on your data object Java class.
    • Doing so, you get a list of options, simply navigate to Google and click the option “Generate Cloud Endpoint Class”.
  • For creating the library:
    • Right click on the backend application of your App Engine.
    • Now, click on Google and select the option “Generate Cloud Endpoint Library”.
# How to define Endpoints in the backend While working with the code of the App Engine application, you can either group it into API or model.
  • Model – are used to include those resources in a class that would be under the impact of your API. For instance, if your app features spiders with wings.
Code Snippet: public class Spider {private int wingLength; public int getWingLength() { return wingLength;} public void setWingLength(int wingLength) { this.wingLength = wingLength;}}
  • API – features those methods that are capable of affecting the model objects, and a client application can also access these methods. The API codes can be written as either of the following two ways.
    • Either you can create a new class say SpiderEndpoint.java corresponding to the file Spider.java.
    • Or you may create the Lava class on your own and limit its access to yourself.
Code Snippet for an Endpoint class: @Api(name = “spider”) public class SpiderEndpoint {@ApiMethod(name = “spider.growWing”, httpMethod = HttpMethod.POST) public void growWing(Spider spider) {// grows the spider’s wing // calculates growth, updates the Spider in Datastore} private int calculateWingGrowth(Spider spider) { // this method is a private utility method // it will NOT be visible in your API}} # How to consume Endpoints from the Client The functions that are defined by you in the backend code is usually represented as a new code and this will come to the client only after the Cloud Endpoint Library has been created. The main difference between the newly created and previous class will be that the new class will extend the GenericJson class to deliver it as JSON. Code Snippet of newly-created Spider class:public final class Spider extends GenericJson {private int wingLength;public int getwingLength() {return wingLength;}public Spider setWingLength(int wingLength) {this.wingLength = wingLength;return this;}} You can conveniently integrate it in an Android client, as the API client code features JSON request classes and several methods. Code Snippet of SpiderEndpoint.java class:public Spider spider() {…}public class Spider {public GrowWing growWing(Spider spider) {…}public class GrowWing extends SpiderRequest {private static final String REST_PATH = “growWing”protected GrowWing(Spider spider) {…}…}} For implementing this Endpoint, one can use the following code of line – service.spider().growWing(spider).execute(); If one would like to implement any change in the created library of your client application, then it must be made in the backend only. # How to design your API Here, you need to follow certain specific rules for creating Model, implementing API methods, supported variable types, return types and parameter types. Some of the rules are stated below-
  • It supports both Enums and Boolean.
  • The classes of your Model must be serializable, include a zero-argument constructor and deploy getters and setters.
  • Your Model classes may also contain other instance methods.
  • While naming the attribute for @Api annotation, one must start with a lowercase character.
  • Your Endpoint classes will not support your static methods.
  • It will not support the overloaded methods in a class.
  • The created client library will contain alphabetically-organized method parameters.
  • Parameter type and return type must be serializable.
  • Subclass return types will create the interface type.
# How to create your Library The installed Google plugin for Eclipse will facilitate with few features while creating your Cloud Endpoints API library. Let’s have a look at some of its features:
  • By making appropriate tweaks in your Eclipse project settings, you will be able to link your already existing App Engine application to the Android front-end.
  • It allows one to conveniently begin from the scratch, by proving auto configuration links.
  • For testing locally, it will create a local development environment.
  • Whenever a major change has been made, or in fact, after a set period, it will recreate your library.
  • It is also capable of resolving the errors by using the Eclipse’s Error Log while creating the Cloud Endpoint Library.
# How to make the Model Datastore-compliant You can use the handy persistent storage of the Datastore via Cloud Endpoints. There are several Datastore types and properties, let’s have a look at some of its features:
  • Datastore is not able to make the difference between the sizes for the numbers like int32 and int64.
  • It only supports Collection types (doesn’t support arrays).
  • It treats an empty list as plain old nulls.
The App Engine allows developers to implement various types of Cloud services like Cloud JPA, SQL, Google BigQuery, etc. Wrapping Up: Reap the benefits of App Engine to develop and host API via the shared web backend, and this can be easily accomplished by using Google Cloud Endpoints. Talk to your client conveniently while developing your application logic by using its flexible tools and libraries that offer client-server interaction protocols.   Author Signature: Lucie Kruger is an eminent Senior Content Editor and IT consultant for www.Mobiers.com – Mobile App Development Service provider. You can also contact her, if you are looking forward to Hire mobile Application Developer.