简体   繁体   English

如何在页面加载中访问Google Feed API返回的值?

[英]how to access values returned by google feed api in page load?

am currently using google feed api to get feed links dynamically. 目前正在使用google feed api动态获取feed链接。 am trying to use the results returned by the api to create rss feed for my website..Now the problem is api function call takes place only after the page load so i cant access the url values returned by the api in page load function.. 我试图使用api返回的结果为我的网站创建rss feed。.现在的问题是api函数调用仅在页面加载后发生,因此我无法访问页面加载函数中api返回的url值。

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
   <script type="text/javascript" src="https://www.google.com/jsapi?key=ABQIAAAAxII5vpTAk5gatTPIMAFoCxStIDvCAqMV0_KActMytIL1qEezxxQeBqRzIurcFfnrUgG2YMlC07VgbQ"></script>
    <script type="text/javascript">

        google.load("feeds", "1", { "callback": OnLoad });

        function OnLoad() {
            // Query for president feeds on cnn.com
            var query = 'atlanta bridal shows';
            google.feeds.findFeeds(query, findDone);
        }

        function findDone(result) {
            // Make sure we didn't get an error.
            if (!result.error) {
                // Get content div
                var content = document.getElementById('content');
                var html = '';
                var submenu = new Array()
                // Loop through the results and print out the title of the feed and link to
                // the url.
                for (var i = 0; i < result.entries.length; i++) {
                    var entry = result.entries[i];
                    html += '<p><a href="' + entry.url + '">' + entry.title + '</a></p>';
                    submenu[i] = entry.url;
                }
                content.innerHTML = html;

                document.getElementById('<%= Hidden1.ClientID %>').value = submenu;
            }
        }

        google.setOnLoadCallback(OnLoad);
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:HiddenField ID="Hidden1" runat="server" ondatabinding="Hidden1_DataBinding" 
        onunload="Hidden1_Unload" onvaluechanged="Hidden1_ValueChanged" />
<div id="content"></div>

</asp:Content>

pageload event: 页面加载事件:

 protected void Page_Load(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        string path = Request.Url.GetLeftPart(UriPartial.Authority) +
               VirtualPathUtility.ToAbsolute("~/user/feed.htm");

        Stream stream = client.OpenRead(path);
        StreamReader sr = new StreamReader(stream);
        string content = sr.ReadToEnd();

          }

What should i do to access the values returned by the api in page load?? 我应该如何访问页面加载中api返回的值?

First of all you have to create feed.htm and put above code in it. 首先,您必须创建feed.htm并将上面的代码放入其中。

feed.htm (which is located at root of web-app) feed.htm(位于Web应用程序的根目录)

<script type="text/javascript" src="https://www.google.com/jsapi?key=ABQIAAAAxII5vpTAk5gatTPIMAFoCxStIDvCAqMV0_KActMytIL1qEezxxQeBqRzIurcFfnrUgG2YMlC07VgbQ"></script>
<script type="text/javascript">

    google.load("feeds", "1", { "callback": OnLoad });

    function OnLoad() {
        // Query for president feeds on cnn.com
        var query = 'atlanta bridal shows';
        google.feeds.findFeeds(query, findDone);
    }

    function findDone(result) {
        // Make sure we didn't get an error.
        if (!result.error) {
            // Get content div
            var content = document.getElementById('content');
            var html = '';
            var submenu = new Array()
            // Loop through the results and print out the title of the feed and link to
            // the url.
            for (var i = 0; i < result.entries.length; i++) {
                var entry = result.entries[i];
                html += '<p><a href="' + entry.url + '">' + entry.title + '</a></p>';
                submenu[i] = entry.url;
            }
            content.innerHTML = html;

            document.getElementById('<%= Hidden1.ClientID %>').value = submenu;
        }
    }

    google.setOnLoadCallback(OnLoad);
</script>
<div id="content"></div>

Then after in page_load event of .aspx page use System.Net.WebClient class methods to request the feed.htm . 然后,在.aspx页面的page_load事件之后,使用System.Net.WebClient类方法请求feed.htm

TestFeed.aspx (which is located at root of web-app) TestFeed.aspx(位于Web应用程序的根目录)

Markup: 标记:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>

Code-behind: 后台代码:

protected void Page_Load(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        string path = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/feed.htm");

        Stream stream = client.OpenRead(path);
        StreamReader sr = new StreamReader(stream);
        //To view the result
        Label1.Text = sr.ReadToEnd();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM