Sunday, June 21, 2015

Google Cloud Endpoints: Creating New Endpoint

This blog post is about using Google cloud endpoint and Adding new endpoint to existing backend project by using android studio. In order to learn more about Google cloud endpoints with Android use the references.

Integrate Cloud Endpoint Module To Project

  • File > New Module
  • Under Choose Module Type select Google Cloud Module and click Next
  • In next screen, select App Engine Java Endpoint Module for the Module type, give a Module Name and a Package name.
  • Click Finish
After Gradle sync finish new module can be seen with the name that has been given as Module Name. Google Cloud Endpoint module has example with MyBean.java, MyEndpoint.java classes. Select  Cloud endpoint module in run configuration and click run button. This endpoint can be seen in API Explorer.

After Android Studio Console display “INFO: Dev App Server is now running”.
  • Go to this http://localhost:8080/
  • Click on Google Cloud Endpoints API Explorer, It ll direct you to this link
    • https://apis-explorer.appspot.com/apis-explorer/?base=http%3A%2F%2Flocalhost%3A8080%2F_ah%2Fapi#p/
  • Above link is the address of API Explorer for the services that is running on the localhost
    • If services are not displayed click on the shield on Chromes address bar
    • It ll show a pop up with message “This page is trying to load scripts from unauthorized sources”, click on load unsafe scripts.
    • After that myApi API can be seen under services
  • API can be tested by clicking on the API.

Adding A New Endpoint

New endpoint is used to calculate sum of two floats. So This API need two java classes they are CalculatorEndpoint.java and ResultBean.Java. Both are under same package of Cloud Module.

ResultBean.java

package com.example.nipun.myapplication.backend;

/**
 * Created by nipun on 6/21/15.
 */
public class ResultBean {
    float result;

    public float getResult() {
        return result;
    }

    public void setResult(float result) {
        this.result = result;
    }
}

CalculatorEndpoint.java

package com.example.nipun.myapplication.backend;

/**
 * Created by nipun on 6/21/15.
 */

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;

import javax.inject.Named;

@Api(name = "calculatorApi",
        version = "v1",
        namespace = @ApiNamespace(
                ownerDomain = "backend.myapplication.nipun.example.com",
                ownerName = "backend.myapplication.nipun.example.com",
                packagePath = ""))
public class CalculatorEndpoint {


    @ApiMethod(name = "addTwoNumbers")
    public ResultBean addTwoNumbers(@Named("num1") float num1, @Named("num2") float num2){
        float result = num1 + num2;

        ResultBean resultBean = new ResultBean();
        resultBean.setResult(result);

        return resultBean;
    }


}

In order to use calculatorApi add fully qualified class name of  CalculatorEndpoint.java to the web.xml file of backend module as below. web.xml can be located in <backend_module_name>/src/main/webapp/WEB-INF/web.xml. backend_module_name is the name of the Google Cloud Module.

    <servlet>
        <servlet-name>SystemServiceServlet</servlet-name>
        <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
        <init-param>
            <param-name>services</param-name>
            <param-value>
                com.example.nipun.myapplication.backend.MyEndpoint,
                com.example.nipun.myapplication.backend.CalculatorEndpoint
            </param-value>
        </init-param>
    </servlet>

Run the cloud backend module again and go to API explorer. Now two numbers can be added with calculatorApi.

References

  • https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints

Saturday, June 20, 2015

Rendering Problems: The following class could not found

Problem

In android studio, after a project was created a message was displayed with following content.

The following class could not found: android.support.v7.internal.widget.actionbaroverlaylayout


Solution

Buld > Rebuild Project

Go to build menu then rebuild the project.