我一直在研究有关如何从 Youtube 获取数据的信息。基本上我想做的是从播放列表(例如:http://gdata.youtube.com/feeds/api/playlists/6A40AB04892E2A1F)中获取有关视频的一些信息(标题、描述和缩略图 URL)。我能够使用此代码 fragment (我从另一个问题借来的)检索标题:
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 和视频描述?
提前致谢。
最佳答案
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
(...)
关于java - 使用 gdata 检索 Youtube 缩略图 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6538531/