0

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

http://gdata.youtube.com/feeds/api/videos/1EEFydL6ooA/comments?start-index=1&max-results=50

当我发送此 URL 时,我收到com.google.gdata.util.ParseException [Line 1, Column 279] Invalid root element, expected (namespace uri:local name) of ( http://www.w3.org/2005/ Atom:entry ),找到 ( http://www.w3.org/2005/Atom:feed

实际上,当我的 URL 是“ 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: [Line 1, Column 101152, element yt:state] Invalid value for attribute : 'name' ”,这是由于库错误。

谢谢

4

1 回答 1

0

I can't try your code. It looks like the PHP library. It seems to me, that your using the wrong class. The url at the start is about comments, although you refer to it as a videoEntry.

(1) This is about comments (since you append "/comments"):

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

(2) YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(str));

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

(3) Although you name it "videoEntry" it is about "comments", due to the value of the url:

String videoEntryUrl = youtubeQuery.getUrl().toString();

VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class);

Instead of the above, maybe try something like:

String myUrl = youtubeQuery.getUrl().toString();  // only another name

CommentFeed commentFeed = service.getFeed(new URL(myUrl), CommentFeed.class); // Feed response

BTW: In the PHP library it detects the class of the feed by itself and the second parameter can be left null. Like: CommentFeed commentFeed = service.getEntry(new URL(myUrl));

于 2013-08-29T12:07:22.007 回答