【问题标题】:Retrieving Youtube Thumbnail URLs using gdata?使用 gdata 检索 Youtube 缩略图 URL?
【发布时间】:2011-06-30 17:13:40
【问题描述】:

我一直在研究有关如何从 Youtube 获取数据的信息。基本上我想做的是从播放列表(例如:http://gdata.youtube.com/feeds/api/playlists/6A40AB04892E2A1F)中获取有关视频的一些信息(标题、描述和缩略图 URL)。我能够使用此代码 sn-p 检索标题(我从另一个问题中借用):

String featuredFeed = "http://gdata.youtube.com/feeds/api/playlists/6A40AB04892E2A1F";

url = new URL(featuredFeed);

URLConnection connection;
connection = url.openConnection();

HttpURLConnection httpConnection = (HttpURLConnection) connection;

int responseCode = httpConnection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {
    InputStream in = httpConnection.getInputStream();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document dom = db.parse(in);
    Element docEle = dom.getDocumentElement();

    NodeList nl = docEle.getElementsByTagName("entry");
    // NodeList nl2 = ;
    if (nl != null && nl.getLength() > 0) {
        for (int i = 0; i < nl.getLength(); i++) {

            Element entry = (Element) nl.item(i);
            Element title = (Element) entry.getElementsByTagName(
                    "title").item(0);

            String titleStr = title.getFirstChild().getNodeValue();

            Log.i("TEST LOG", "TITLES: " + titleStr);

        }
    }
}

但是我不太清楚如何检索缩略图 URL。我看过标签,但我不知道如何从节点列表中调用它。 谁能告诉我如何使用这种方法检索视频的缩略图 URL 和视频描述?

提前致谢。

【问题讨论】:

    标签: java android api youtube nodelist


    【解决方案1】:
    Log.i("TEST LOG", "TITLES: " + titleStr);
    (...)
                        Element groupNode = (Element)entry.getElementsByTagNameNS("*", "group").item(0);
    
                        NodeList tNL = groupNode.getElementsByTagNameNS("*", "thumbnail");
    
                        for (int k = 0; k < tNL.getLength(); k++) {
                            Element tE = (Element)tNL.item(k);
    
                            if (tE != null) {
                                System.out.println("Thumbnail url = " + tE.getAttribute("url"));
                            }
                        }
    
                        NodeList dNL = groupNode.getElementsByTagNameNS("*", "description");
    
                        for (int k = 0; k < dNL.getLength(); k++) {
                            Element tE = (Element)dNL.item(k);
    
                            if (tE != null) {
                                System.out.println("Description = " + tE.getFirstChild().getNodeValue());
                            }
                        }
    
                    } // end for
    
    (...)
    

    【讨论】:

    • 请注意,您不必再这样想了。看看像 HtmlUnit 这样的库和 XPath 查询的使用,以避免像我们在这里所做的那样痛苦的迭代。
    猜你喜欢
    • 2011-10-09
    • 2012-06-27
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 2011-10-12
    • 2015-07-05
    • 2013-08-10
    • 2013-02-01
    相关资源
    最近更新 更多