Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback custom method call from Android

I am looking for example where I can call loopback's custom method from Android. To explain more, lets say I have a method on server side with name "greet(name)" that will greet someone. I want to invoke that from Android. Any example, or link is ok.

Thanks in advance.

Jahid

like image 815
Jahid Shohel Avatar asked May 07 '26 07:05

Jahid Shohel


1 Answers

In the examples below, I'll assume your model is called Greeter and the static method Greeter.greet is invoked 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 developers of LoopBack, loopback-sdk-android is one of my specialisations.

like image 188
Miroslav Bajtoš Avatar answered May 09 '26 03:05

Miroslav Bajtoš



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!