Using Cloud Code SDK in Android Kotlin

Backendless Version (Pro)

Client SDK (Android/ CloudCode)

Application ID = DD9FC30D-3A6F-AB4C-FF9C-24DC92E73100

Expected Behavior

We have created backend API’s using the Cloud Code service and want to use the SDK created from this to pull data into our Android app. After creating a CloudCode API to pull in all the objects from our data table, we downloaded the SDK and started to right the code to properly invoke it.

We have followed the invoking example that is given with the SDK, when going to invoke the function, we put the 4 parameters that our Async API is expecting so that the value can be returned. The code then asks for a fifth parameter ‘callback’, we are unable to figure out what parameter needs to be passed here so that we can have the correct JSON object returned.

I included the example below, as well as the async code that we have been using. Please advise.

Code from cloud code SDK that we are trying to invoke

    public static void getAllChurchesAsync(int limit, int offset, java.lang.String q, int build, AsyncCallback<com.ns804.hereiam.churches> callback)
    {
        Object[] args = new Object[]{limit, offset, q, build};
        Backendless.CustomService.invoke( SERVICE_NAME, "getAllChurches", args, callback);
    }

Code that we created in Kotlin to invoke this method

        object InvocationExample {
            @JvmStatic
            fun main(args: Array<String>) {
                ChurchService.initApplication()
                ChurchService.getInstance()
                // invoke methods of you service
                val result: Any = ChurchService.getAllChurchesAsync(limit, offset, q, 0, AsyncCallback<churches> )
                println( result );
            }
        }

AsyncCallback<churches is the placeholder that we are using for the callback parameter, but it has not been working.

Thanks.

Hi @Will_York ,

Could you please post here a stack-trace for the exception which you receive when your custom service is invoked?

Regards, Andriy

Hey Andy,

I cannot invoke the method yet so I do not have a stack-trace for the exception. I am trying to figure out what parameter needs to be passed in ‘callback’. I attached images of the two errors that popup when I try to invoke the method.

Any information helps!

@Will_York ,

You should implement that interface with your own class. That interface provides two methods - one to handle received data and the other to handle error. You can use anonymous class there like in this example for Java

Regards, Andriy

Hello Andriy thanks for the response. The example you sent along is for basic object retrieval. We have created API’s in the cloud code service within Backendless and need to invoke it in our Android app. The code that is packaged in the Zip file does not return a JSON object for our client side to use.

object InvocationExample {
            @JvmStatic
            fun main(args: Array<String>) {
                ChurchService.initApplication()
                ChurchService.getInstance()
                // invoke methods of you service
                val result: Any = ChurchService.getAllChurchesAsync(limit, offset, q, 0, AsyncCallback<churches> )
                println( result );
            }
        }

How do we get it to return a JSON object?

Hi Will,

By looking at the signature of the service call below, I see that it returns an instance of the churches object - see the generic type of the AsyncCallback. The method is invoked asynchronously, therefore the result variable, is not the actual result returned by the service.

Regarding the AsyncCallback interface, it is documented here:
https://backendless.com/docs/android/sync_and_async_calls.html

What you can do is create an anonymous class that implements the interface. For example:

ChurchService.initApplication()
ChurchService.getInstance()
// invoke methods of your service
ChurchService.getAllChurchesAsync(limit, offset, q, 0,  new AsyncCallback<churches>() {
  public void handleResponse( churches response ) {
    // this is where you get the actual response
  }

  public void handleFault( BackendlessFault fault ) {
    // an error has occurred, the error code can be retrieved with fault.getCode()
  }
});

Hope this helps.

Mark