问题使用JavaScript解析RSS订阅 [英] Problem parsing Rss feed using javascript

查看:111
本文介绍了问题使用JavaScript解析RSS订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读两个RSS Feed网址定期并显示最新的饲料标题我的Andr​​oid应用内的通知。

由于setInterval函数被调用每8000毫秒我要确保有相同的标题没有重复的通知。

当我做getrss功能,我能看到XML对象内的console.log,但
我无法看到被输出的任何数据的 XML变种,变种进入或VAR标题
普莱舍指出的是我可能会继续。

  / * ----------------------全局变量------------ ---------- * /    VAR Rss1_title;
    VAR Rss2_title;/ * ----------------------结束全局变量------------------- * // * ---------拉最新的RSS订阅标题第一个URL:---------- * /    功能update_rss1_feeds(){
    变种NEW_TITLE = getRss(http://yofreesamples.com/category/real-freebies/feed/);
        如果(Rss1_title!= NEW_TITLE)//显示通知            navigator.notification.alert(
                NEW_TITLE,//消息
                新的RSS条目',//称号
                新的RSS条目'// BUTTONNAME
            );        Rss1_title = NEW_TITLE;
    }
/* - - - - - 结束 : - - - - - *// * ---------拉最新的RSS订阅标题第二网址:--------- * /    功能update_rss2_feeds(){
    变种NEW_TITLE = getRss(http://yofreesamples.com/category/free-coupons/feed/);
        如果(Rss2_title!= NEW_TITLE)//显示通知            navigator.notification.alert(
                NEW_TITLE,//消息
                新的RSS条目',//称号
                新的RSS条目'// BUTTONNAME
            );        Rss2_title = NEW_TITLE;
    }
/* - - - - - 结束 : - - - - - */

这是getrss功能是我解析RSS提要。我可能会做在这里有些不对劲。

 函数getRss(URL){
如果(URL == NULL)返回false;            //创建谷歌资讯提供API地址
            VAR API =htt​​p://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=+ EN codeURIComponent(URL);
            API + =&放大器;输出= json_xml            VAR新条目;
            // 发送请求
            $ .getJSON(API,功能(数据){
                的console.log('TESTDATA',数据); //响应是xmlobjest
                //检查错误
                如果(data.responseStatus == 200){                    VAR饲料= data.responseData.feed;
                    如果(!供稿){
                        返回false;
                    }                    //获取XML数据介质(未使用parseXML如需要1.5+)
                    VAR XML = getXMLDocument(data.xmlString);
                    VAR xmlEntries = xml.getElementsByTagName('项目');
                    变种条目= feeds.entries [0];
                    VAR标题= entry.title;
                    的console.log(测试,标题); //未显示它,似乎它在if语句内不打算
                    返回称号;
                }其他{                };            });}

这是函数调用。

 的setInterval('update_rss1_feeds()',7000);
的setInterval('update_rss2_feeds()',8000);


解决方案

当我访问:

  http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q= [RSS订阅源URL]放大器;输出= json_xml

我得到一个错误坏或丢失回调或上下文,如果我把我们的回调参数正常工作

  http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q= [RSS订阅源URL]放大器,输出= json_xml

I am trying to read two Rss feed urls at regular intervals and display the latest feed title as a notification inside my android app.

As the setinterval function is being called every 8000 ms I want to make sure there are no duplicate notifications of the same title.

When I do a console.log inside the getrss function I am able to see the xml object, but I am not able to see any data being outputted for var xml , var entry or var title . Plese point out were I might be going on.

/* ---------------------- global variables ---------------------- */

    var Rss1_title;
    var Rss2_title;

/* ---------------------- end global variables------------------- */

/* --------- pull latest Rss feed title for first url: ---------- */  

    function update_rss1_feeds(){
    var new_title = getRss("http://yofreesamples.com/category/real-freebies/feed/");
        if(Rss1_title != new_title)

//display the notification

            navigator.notification.alert(
                new_title,  // message
                'New Rss Entry',                 // title
                'New Rss Entry'                  // buttonName
            );

        Rss1_title = new_title;             
    }
/* --------- end : --------- */

/* --------- pull latest Rss feed title for second url: --------- */  

    function update_rss2_feeds(){
    var new_title = getRss("http://yofreesamples.com/category/free-coupons/feed/");
        if(Rss2_title != new_title)

//display the notification

            navigator.notification.alert(
                new_title,  // message
                'New Rss Entry',                 // title
                'New Rss Entry'                  // buttonName
            );

        Rss2_title = new_title;             
    }
/* --------- end : --------- */

This is the getrss function were I parse the Rss feed. I might be doing something wrong over here.

function getRss(url){
if(url == null) return false;

            // Create Google Feed API address
            var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" + encodeURIComponent(url);
            api += "&output=json_xml"

            var newEntry;
            // Send request
            $.getJSON(api, function(data){
                console.log('testdata',data); // the response is xmlobjest
                // Check for error
                if (data.responseStatus == 200) {

                    var feeds = data.responseData.feed;
                    if (!feeds) {
                        return false;
                    }

                    // Get XML data for media (parseXML not used as requires 1.5+)
                    var xml = getXMLDocument(data.xmlString);
                    var xmlEntries = xml.getElementsByTagName('item');
                    var entry = feeds.entries[0];
                    var title = entry.title;
                    console.log('test',title); // it is not being displayed , it seems its not going inside the if statement
                    return title;
                }else {

                };              

            });             

}

This is the Function call.

setInterval('update_rss1_feeds()',7000);
setInterval('update_rss2_feeds()',8000);

解决方案

When I access:

http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=[an rss feed url]&output=json_xml 

I get an error "bad or missing callback or context", if I take our the callback parameter it works fine

http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=[an rss feed url]&output=json_xml 

这篇关于问题使用JavaScript解析RSS订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆