我是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"/>
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);
}
所有其他适配器文件与“HTTP适配器”练习和代码示例中的RSSReader完全相同。我正在通过Android测试该示例。
请求响应总是来自'getFeedsFailure()'。所以我需要帮助来解决问题,谢谢!
答案 0 :(得分:1)
我测试了你的代码,你需要修复两件事来使适配器工作。
</xsl:stylesheet>
添加到您的filtered.xsl 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();