我想从youtube视频中获取所有评论(最多999)。这是我要发送的网址
http://gdata.youtube.com/feeds/api/videos/1EEFydL6ooA/comments?start-index=1&max-results=50
当我发送此网址时,我收到 com.google.gdata.util.ParseException [第1行,第279列]无效的根元素,期望(名称空间uri:本地名称)为(http://www.w3.org/2005/Atom:entry) ,找到(http://www.w3.org/2005/Atom:feed
实际上,当我的网址为“http://gdata.youtube.com/feeds/api/videos/1EEFydL6ooA”时,我收到25条评论。但是由于这只是一个视频,我无法设置max-results和start-index参数。我的代码是:
String str = "http://gdata.youtube.com/feeds/api/videos/" + videoId
+ "/comments";
YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(str));
youtubeQuery.setMaxResults(50);
youtubeQuery.setStartIndex(1);
String videoEntryUrl = youtubeQuery.getUrl().toString();
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl),
VideoEntry.class);
if (videoEntry.getComments() != null) {
String commentUrl = videoEntry.getComments().getFeedLink()
.getHref();
System.out.println(commentUrl);
CommentFeed commentFeed = service.getFeed(new URL(commentUrl),
CommentFeed.class);
for (int i = 0; i < commentFeed.getEntries().size()
&& commentFeed.getEntries().get(i) != null; i++) {
String author=commentFeed.getEntries().get(i).getAuthors().get(0)
.getUri().substring(41)
String commentId=commentFeed.getEntries().get(i).getId().substring(47);
String comment=commentFeed.getEntries().get(i).getPlainTextContent();
为什么我会收到parseException?也许是因为这段代码相应地工作VideoEntry对象和解析是以这种方式完成的。有没有像CommentEntry这样的东西?如果有的话,怎么能初始化呢?
请注意,我的异常不是“com.google.gdata.util.ParseException:[第1行,第101152列,元素yt:state]属性的值无效:'name'”这是由于错误的库。
由于
答案 0 :(得分:0)
我无法尝试您的代码。它看起来像PHP库。 在我看来,你使用了错误的课程。一开始的网址是关于评论的,尽管您将其称为videoEntry。
(1) 这是关于评论(因为你追加“/ comments”):
String str = "http://gdata.youtube.com/feeds/api/videos/" + videoId + "/comments";
(2) YouTubeQuery youtubeQuery =新的YouTubeQuery(新网址(str));
youtubeQuery.setMaxResults(50);
youtubeQuery.setStartIndex(1);
(3) 虽然您将其命名为“videoEntry”,但由于网址的价值,它与“评论”相关:
String videoEntryUrl = youtubeQuery.getUrl().toString();
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class);
而不是上述内容,可以尝试类似:
String myUrl = youtubeQuery.getUrl().toString(); // only another name
CommentFeed commentFeed = service.getFeed(new URL(myUrl), CommentFeed.class); // Feed response
BTW:在PHP库中,它自己检测feed的类,第二个参数可以保留为null。喜欢:CommentFeed commentFeed = service.getEntry(new URL(myUrl));