2016年3月22日 星期二

How to Create GCE Module(Make your own API)

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

see the above link,you can learn how to create a api on google cloud,and use in android.

below example show how to add you self method.

public class MyEndpoint {
    private static int jokeAcount = 0;
    final String[] jokeArray = {"joke1", "joke2", "joke3", "joke4", "joke5"};

    /**
     * A simple endpoint method that takes a name and says Hi back
     */
    @ApiMethod(name = "sayHi2")
    public MyBean sayHi2(@Named("name") String name) {
        MyBean response = new MyBean();
        response.setData("Hi, " + name);

        return response;
    }

    @ApiMethod(name = "getJoke")
    public JokeObject getJoke() {
        JokeObject jokeObject = new JokeObject(jokeArray[jokeAcount]);
        jokeAcount++;
        if (jokeAcount >= jokeArray.length) jokeAcount = 0;

        return jokeObject;
    }

}