我一直在研究如何从Youtube获取数据的信息。基本上我想要做的是从播放列表中获取有关视频(标题,描述和缩略图URL)的一些信息(例如:http://gdata.youtube.com/feeds/api/playlists/6A40AB04892E2A1F)。我能够使用此代码片段(我从另一个问题借用)检索标题:
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和视频描述?
提前致谢。
答案 0 :(得分:0)
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
(...)