Как получить любимые видео с клиентской библиотекой Java API Google Api? - PullRequest
1 голос
/ 08 октября 2011

Я пытаюсь получить любимые видео пользователя YouTube с помощью клиентской библиотеки Java API google .Это всего лишь настройка youtube-json-sample .Что я в основном хочу сделать, это напечатать список любимых видео пользователя (в частности, печать заголовка, обновление, описание и т. Д.).Мне удается получить список - но проблема в том, что все (заголовок, описание и т. Д.) Равно нулю!Это происходит только с фаворитами - я проверил это с поиском и наиболее просматриваемыми запросами, и это работало.Это странное поведение только с избранными ... Вот мой код:

private  void run() throws IOException {
        YouTubeClient client = new YouTubeClient();
        showVideos(client);
      }

    private VideoFeed showVideos(YouTubeClient client) throws IOException {

        TextView textView = (TextView) findViewById(R.id.textView);
        View.header(textView, "Get Videos");
        YouTubeUrl url = YouTubeUrl.forVideosFeed();
        // execute GData request for the feed
        VideoFeed feed = client.executeGetVideoFeed(url);
        View.display(textView, feed);
        return feed;
      }

public class YouTubeUrl extends GoogleUrl {

  /** Whether to pretty print HTTP requests and responses. */
  private static final boolean PRETTY_PRINT = true;

  static final String ROOT_URL = "https://gdata.youtube.com/feeds/api";



  YouTubeUrl(String encodedUrl) {
    super(encodedUrl);
    this.alt = "jsonc";
    this.prettyprint = PRETTY_PRINT;
  }

  private static YouTubeUrl root() {
    return new YouTubeUrl(ROOT_URL);
  }

  public static YouTubeUrl forVideosFeed() {
    YouTubeUrl result = root();

    result.getPathParts().add("users"); //the URL is http://gdata.youtube.com/feeds/api/users/username/favorites?v=2
    result.getPathParts().add("liorash1"); //some user name
    result.getPathParts().add("favorites");

    return result;
  }
}

public class YouTubeClient {

  private final JsonFactory jsonFactory = new JacksonFactory();

  private final HttpTransport transport = new NetHttpTransport();

  private final HttpRequestFactory requestFactory;

  public YouTubeClient() {
    final JsonCParser parser = new JsonCParser(jsonFactory);
    requestFactory = transport.createRequestFactory(new HttpRequestInitializer() {

      @Override
      public void initialize(HttpRequest request) {
        // headers
        GoogleHeaders headers = new GoogleHeaders();
        headers.setApplicationName("Google-YouTubeSample/1.0");
        headers.gdataVersion = "2";
        request.setHeaders(headers);
        request.addParser(parser);
      }
    });
  }

  public VideoFeed executeGetVideoFeed(YouTubeUrl url) throws IOException {
    return executeGetFeed(url, VideoFeed.class);
  }

  private <F extends Feed<? extends Item>> F executeGetFeed(YouTubeUrl url, Class<F> feedClass)
      throws IOException {
    HttpRequest request = requestFactory.buildGetRequest(url);
    return request.execute().parseAs(feedClass);
  }
}

public class Item {

  @Key
  String title;

  @Key
  DateTime updated;
}

public class Video extends Item {

  @Key
  String description;

  @Key
  List<String> tags = new ArrayList<String>();

  @Key
  Player player;
}

Есть классы, которые я здесь не поместил, так что это не будет слишком грязно.В любом случае, я получаю вывод:

============== Get Videos ==============

Showing first 6 of 8 videos: 

-----------------------------------------------
Title: null
Updated: null

-----------------------------------------------
Title: null
Updated: null

-----------------------------------------------
Title: null
Updated: null

-----------------------------------------------
Title: null
Updated: null

-----------------------------------------------
Title: null
Updated: null

-----------------------------------------------
Title: null
Updated: null

НЕКОТОРЫЕ ОБНОВЛЕНИЯ:

После некоторого тестирования я обнаружил, что могу получать все проверенные каналы (включая загрузки пользователя), кромефаворитыПроблема, скорее всего, связана с JsonCParser:

return request.execute().parseAs(feedClass);
...