Loopback custom method call from Android

北城以北 提交于 2019-12-04 12:55:45

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!