Loopback custom call method from android

I'm looking for an example where I can call a custom loopback method from Android. To explain more, let's say I have a server side method named "greet (name)" that will greet someone. I want to call this from Android. Any example or link is ok.

Thanks in advance.

Jahid

+3


source to share


1 answer


In the examples below, I'll assume your model is called Greeter

and a static method Greeter.greet

is called via GET /greeters/greet?name=Alex

.

First of all, you need to describe the REST mapping of your method. Then you can call the method using invokeMethod

.

public class GreeterRepository extends ModelRepository<Greeter> {
    public RestContract createContract() {
      RestContract contract = super.createContract();

      contract.addItem(new RestContractItem("/" + getNameForRestUrl() + "/greet", "POST"),
                  getClassName() + ".greet");

      return contract;
    }

    public void greet(name, final VoidCallback callback) {
        invokeStaticMethod("greet", ImmutableMap.of("name", name), new Adapter.Callback() {

            @Override
            public void onError(Throwable t) {
                callback.onError(t);
            }

            @Override
            public void onSuccess(String response) {
                callback.onSuccess();
            }
        });
    }
}

      



See ModelRepository.java and Model.java for examples of methods that parse the response body.

Disclaimer: I am one of the LoopBack developers, loopback-sdk-android is one of my specialties.

+5


source







All Articles