从Youtube视频中检索评论时出现ParseException



我想从youtube视频中获得所有评论(最多999条)。这是我想发送的URL

http://gdata.youtube.com/feeds/api/videos/1EEFydL6ooA/comments?start-index=1&最大结果=50

当我发送此URL时,我得到com.google.gdata.util.ParseException[第1行,第279列]无效的根元素,应为(命名空间uri:本地名称)(http://www.w3.org/2005/Atom:entry),找到(http://www.w3.org/2005/Atom:feed

事实上,当我的URL是"http://gdata.youtube.com/feeds/api/videos/1EEFydL6ooA",我得到了25条评论(如果有的话)。然而,由于这是关于一个单独的视频,我无法设置最大结果和启动索引参数。我的代码是:

    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"的无效值",这是由于错误的库造成的。

感谢

我无法尝试您的代码。它看起来像PHP库。在我看来,你用错了类。开头的url是关于评论的,尽管您将其称为videoEntry。

(1)这是关于评论(因为你附加了"/评论"):

String str = "http://gdata.youtube.com/feeds/api/videos/" + videoId + "/comments";

(2)YouTubeQuery YouTubeQuery=新YouTubeQuery(新URL(str));

youtubeQuery.setMaxResults(50);
youtubeQuery.setStartIndex(1);

(3)虽然你把它命名为"videoEntry",但它是关于"评论"的,因为url的值:

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库中,它自己检测提要的类,第二个参数可以为null。类似:CommentFeed CommentFeed=service.getEntry(新URL(myUrl));

相关内容