Xcihnegn
Xcihnegn

Reputation: 11597

Why worklight adapter does not work?

I am new in IBM Worklight, and trying 'Creating HTTP Adapters' - 'Exercise and code sample', but I could not get feeds, instead always get failure response from 'WL.Client.invokeProcedure'.

My 'FeedsAdapter.js' is here:

var FeedsAdapter = (function(){
function getFeeds(_caller){
    var caller = _caller;

    var invocationData = {
            adapter: "RSSReader_1", 
            procedure: "getFeedsFiltered",
            parameters: []
    };

    WL.Client.invokeProcedure(invocationData, {
        onSuccess: getFeedsSuccess,
        onFailure: getFeedsFailure
    });

    function getFeedsSuccess(data){
        caller.callback(data.invocationResult.Items);
    }

    function getFeedsFailure(){
        alert("Failed to get feeds. Please check backend connectivity and restart the app");
    }
}

return {
    getFeeds: getFeeds
};
}());

filtered.xsl:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:h="http://www.w3.org/1999/xhtml"
            xmlns:dc="http://purl.org/dc/elements/1.1/" >
<xsl:output method="text"/>

<xsl:template match="/">
    {
        'Items': [
            <xsl:for-each select="//item">
                {
                    'title': '<xsl:value-of select="title"/>',
                    'creator': '<xsl:value-of select="dc:creator"/>',
                    'link': '<xsl:value-of select="link"/>',
                    'pubDate': '<xsl:value-of select="pubDate"/>'
                },
            </xsl:for-each>
        ]
    }
</xsl:template>

RSSReader_1.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--  
 Licensed Materials - Property of IBM
 5725-G92 (C) Copyright IBM Corp. 2006, 2012. All Rights Reserved.
 US Government Users Restricted Rights - Use, duplication or 
 disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  
 -->
<wl:adapter xmlns:wl="http://www.worklight.com/integration"
   xmlns:http="http://www.worklight.com/integration/http"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RSSReader_1">

<displayName>RSSReader_1</displayName>
<description>RSSReader_1</description>
<connectivity>
    <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
        <protocol>http</protocol>
        <domain>engadget.com</domain>
        <port>80</port>         
    </connectionPolicy>
    <loadConstraints maxConcurrentConnectionsPerNode="2"/>
</connectivity>

<procedure name="getFeeds"/>

<procedure name="getFeedsFiltered"/>

RSSRearder_1-impl.js:

function getFeeds() {
var input = {
    method : 'get',
    returnedContentType : 'xml',
    path : "rss.xml"
};


return WL.Server.invokeHttp(input);
}

function getFeedsFiltered() {

var input = {
    method : 'get',
    returnedContentType : 'xml',
    path : "rss.xml",
    transformation : {
        type : 'xslFile',
        xslFile : 'filtered.xsl'
    }
};

return WL.Server.invokeHttp(input);
}

All other adapter files exactly same as RSSReader in 'HTTP Adapters 'Exercise and code sample'. I am testing the sample by Android.

The request response always come to 'getFeedsFailure()'. So I need help to figure out problems, thanks!

Upvotes: 2

Views: 525

Answers (1)

ikhnaton
ikhnaton

Reputation: 21

I tested your code and you need to fix two things to make the adapter work.

  1. add </xsl:stylesheet> to the end of your filtered.xsl
  2. change engadget.com to www.engadget.com otherwise you get a 404 not found.

Hope this helps.

For me I got a 404 if I did not add a www in #2 above.

I added your code to the main.js as a quick test and it works fine. I think you may be running into a timing issue where Worklight has not yet completed initialization when you are making your call.

var FeedsAdapter = (function(){
    function getFeeds(_caller){
        var caller = _caller;

        var invocationData = {
                adapter: "RSSReader_1", 
                procedure: "getFeedsFiltered",
                parameters: []
        };

        WL.Client.invokeProcedure(invocationData, {
            onSuccess: getFeedsSuccess,
            onFailure: getFeedsFailure
        });

        function getFeedsSuccess(data){
            console.log(data);
            caller.callback(data.invocationResult.Items);
        }

        function getFeedsFailure(){
            alert("Failed to get feeds. Please check backend connectivity and restart the app");
        }
    }

    return {
        getFeeds: getFeeds
    };

}());

FeedsAdapter.getFeeds();

Upvotes: 1

Related Questions