Ashwini Maddala
Ashwini Maddala

Reputation: 197

Java based Adapter- Could not find the Return value

I am using a java based adapter in worklight. I have a method which returns a string value. I am able to call the function and the result is going to success handler in the adapter, but i am not able to find out anything about the return value. I cant see the returned String anywhere in the response JSON. Can anyone help me with this?

Here is my response JSON:

{"status":200,"invocationContext":null,"invocationResult":{"responseID":"16","isSuccessful":true}}

I have seen the following example http://public.dhe.ibm.com/ibmdl/export/pub/software/mobile-solutions/worklight/docs/Module_05_5_-_Using_Java_in_Adapters.pdf, when i do an "invoke Adapter Procedure" on the code sample, I am getting this result.

{ "isSuccessful": true, "result": -9 } where result is the return value of the java method in the adapter.

But when i do the same thing for my app, i get the following

{ "isSuccessful": true }

Java-adapter.impl code

function getXML() { return {result: com.worklight.javaCode.FileIOPlugin.getXML() }; }

Java class method

public class FileIOPlugin{ public static String getXML() { return "SUCCESS"; } }

function getXML()
{
var invocationData ={
adapter: 'JavaAdapter',
procedure: 'getXML'
};

WL.Client.invokeProcedure(invocationData,{
onSuccess: successHandler,
onFailure: failureHandler
)};

 function successHandler(data) {alert(JSON.stringify(data));}
 function failureHandler(data) {alert("Error to get data");}

Upvotes: 1

Views: 763

Answers (2)

Orlando Rincón
Orlando Rincón

Reputation: 547

I've tried to reproduce your problem in the recently released Worklight 6.0, and I see everything working fine, after a copy&paste of your code.

The only change I did was to add the empty parameters on the invocationData object used to invoke the adapter method.

This is my exact code:
FileIOPlugin.java (under server/conf, in a package com.worklight.javacode)

package com.worklight.javacode;

public class FileIOPlugin {
    public static String getXML() {
        return "SUCCESS";
    }
}

JavaAdapter.xml (HTTP adapter definition, under adapters folder)

<?xml version="1.0" encoding="UTF-8"?>
<wl:adapter name="JavaAdapter"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:wl="http://www.worklight.com/integration"
    xmlns:http="http://www.worklight.com/integration/http">

    <displayName>JavaAdapter</displayName>
    <description>JavaAdapter</description>
    <connectivity>
        <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
            <protocol>http</protocol>
            <domain>rss.cnn.com</domain>
            <port>80</port> 
            <!-- Following properties used by adapter's key manager for choosing specific certificate from key store  
            <sslCertificateAlias></sslCertificateAlias> 
            <sslCertificatePassword></sslCertificatePassword>
            -->     
        </connectionPolicy>
        <loadConstraints maxConcurrentConnectionsPerNode="2" />
    </connectivity>

    <procedure name="getXML"/>  
</wl:adapter>

JavaAdapter-impl.js (next to JavaAdapter.xml)

function getXML() {
    return {
        result : com.worklight.javacode.FileIOPlugin.getXML()
    };
}

I called my app javaAdapterApp, hence these file names:

javaAdapterApp.js (under apps/javaAdapterApp/common/js)

function wlCommonInit(){
}

function getXML() {
    var invocationData = {
        adapter : 'JavaAdapter',
        procedure : 'getXML',
        parameters : []
    };

    WL.Client.invokeProcedure(invocationData, {
        onSuccess : successHandler,
        onFailure : failureHandler
    });
}

function successHandler(data) {
    alert(JSON.stringify(data));
}
function failureHandler(data) {
    alert("Error to get data");
}

And finally javaAdapterApp.html (under apps/javaAdapterApp/common)

<!DOCTYPE HTML>
<html>
        <head>
            <meta charset="UTF-8">
            <title>javaAdapterApp</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
            <link rel="shortcut icon" href="images/favicon.png">
            <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
            <link rel="stylesheet" href="css/javaAdapterApp.css">
            <script>window.$ = window.jQuery = WLJQ;</script>
        </head>
        <body id="content" style="display: none;">
            <button onClick="getXML()">GET XML</button>

            <script src="js/initOptions.js"></script>
            <script src="js/javaAdapterApp.js"></script>
            <script src="js/messages.js"></script>
        </body>
</html>

I ran it in the test server, and the result of JSON.stringify(data) in the success handler looks like:

{"status":200,"invocationContext":null,"invocationResult":{"responseID":"9","result":"SUCCESS","isSuccessful":true}}

There is the "SUCCESS" String you are looking for in the invocationResult.result.

Hope this helps
Orlando

Upvotes: 0

Raanan Avidor
Raanan Avidor

Reputation: 3583

The return needs to be an object.

Upvotes: 1

Related Questions