Search code examples
javaibm-mobilefirstmobilefirst-adapters

IBM MobileFirst: Invoking adapter from Java- InvokeProcedure


I'm trying to invoke a adapter from Java which is working fine with

DataAccessService service = WorklightBundles.getInstance().getDataAccessService();
ProcedureQName procedure = new ProcedureQName(adapterName,adapterProc);
service.invokeProcedure(procedure, String);

The issue comes in when the String in the above invokeProcedure has to be passed as an input to the invoked adapter. When there is no argument required it works, but when i pass a String(eg. "Hello") it breaks. The reason is, when it is invoked the value passed in the String argument is not parsed by the Javascript adapter-impl.js. and thus an error is thrown saying

adapter name/procedurename cannot be invoked because of invalid characters('H' line 1) in the String argument passed in runtime

I have tried to use the JSONArray/JSONObject in com.ibm.json.java.* package, but the error persists.

So tried using the other overload of invokeProcedure(ProcedureQName,JSONArray,long nRequestID). Something like,

service.invokeProcedure(procedure, objArray,-1)

Surprisingly this is working fine for now. I wanted to understand whether,

  1. is the right approach for this scenario and
  2. will there be any issues in using the 3 argument overload of invokeProcedure in future ?

Please help.


Solution

  • You need to pass arguments as a stringified array. E.g.

    service.invokeProcedure(procedure, "[1,2,3,4]");
    service.invokeProcedure(procedure, "[1,2,'three','four']");
    service.invokeProcedure(procedure, "[1,2,true,false]");
    

    in case of a single argument just do an array with one single value

    service.invokeProcedure(procedure, "['myStringParam']");
    

    Another option is to create JSONArray object and stringify it.