Search code examples
xml-parsingyoutube-apigdata

ParseException while retrieving comments from Youtube video


I want to get all comments (up to 999) from a youtube video. This is the URL that i want to send

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

When I send this URL i am getting 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), found (http://www.w3.org/2005/Atom:feed

Actually, when my URL was "http://gdata.youtube.com/feeds/api/videos/1EEFydL6ooA", I was getting 25 comments if any. However since this is about one single video, I was not able to set max-results and start-index parameter. My code is :

    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();

Why am I getting parseException? Maybe because this code works accordingly VideoEntry object and parsing is done in this way. Is there something like CommentEntry? How can initiliaze it if any?

Note that my exception is not " com.google.gdata.util.ParseException: [Line 1, Column 101152, element yt:state] Invalid value for attribute : 'name' " which is due to wrong library.

Thanks


Solution

  • 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));