【问题标题】:Why worklight adapter does not work?为什么工作灯适配器不起作用?
【发布时间】:2014-07-10 12:27:54
【问题描述】:

我是 IBM Worklight 的新手,正在尝试“创建 HTTP 适配器”-“练习和代码示例”,但我无法获得提要,而是总是从“WL.Client.invokeProcedure”获得失败响应。

我的“FeedsAdapter.js”在这里:

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"/>

RSSRarder_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);
}

所有其他适配器文件与“HTTP 适配器”练习和代码示例中的 RSSReader 完全相同。我正在通过 Android 测试示例。

请求响应总是到达“getFeedsFailure()”。所以我需要帮助来解决问题,谢谢!

【问题讨论】:

  • 您是否部署了适配器?您是否部署了应用程序?使用适配器 XML 编辑问题。使用来自 LogCat 的日志编辑问题。
  • 您也可以直接在 Worklight Studio 中测试您的适配器,方法是右键单击适配器并选择 Run As -> Invoke Adapter Procedure。在这里您可以选择您的程序,然后输入您的参数并单击运行。这应该可以帮助您找出导致问题的确切原因
  • 是的,我已经部署了适配器,还部署了应用程序。我刚刚用适配器 XML 编辑了我的问题。但没有 LogCat,因为我在没有 USB 连接的 Android 设备上测试应用程序
  • 这可能是由于您的设备无法连接到 WL 服务器造成的吗?如果与 WL 服务器的连接不可用,则可能会调用失败回调。检查您的防火墙/IP 地址设置,以确保您可以从您的设备连接到服务器。最好检查一下您是否可以从移动浏览器访问 WL 服务器控制台。
  • 嗨 MikeOrtman,我同意设备无法连接到 WL 服务器,因为我可以“调用适配器过程”从 Eclipse 获取结果。那么我该如何建立这种联系呢?

标签: android ibm-mobilefirst worklight-adapters


【解决方案1】:

我测试了您的代码,您需要修复两件事以使适配器正常工作。

  1. &lt;/xsl:stylesheet&gt; 添加到您的filtered.xsl 的末尾
  2. engadget.com 更改为 www.engadget.com 否则会出现 404 未找到。

希望这会有所帮助。

如果我没有在上面的 #2 中添加 www,我会得到 404。

我将您的代码添加到 main.js 作为快速测试,它运行良好。我认为您在拨打电话时可能会遇到时间问题,即 Worklight 尚未完成初始化。

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();

【讨论】:

  • 我有 ,但粘贴时这里不见了。我不认为是域 'engadget.com' 的问题,因为我可以从 Eclipse 调用该过程并获得正确的响应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 2011-04-26
  • 2016-09-15
相关资源
最近更新 更多