Exemple #1
0
  public ArrayList<YoutubeVideo> loadMore(String offset, String keyword, int max) {
    try {
      query = youtube.search().list("id,snippet");
      query.setKey(this.KEY);
      query.setOrder("date");
      query.setType("video");
      query.setChannelId(this.channel_id);
      if (keyword != null) {
        query.setQ(keyword);
      }
      query.setPageToken(offset);
      query.setMaxResults((long) max);
      query.setFields(queryParams);
    } catch (IOException e) {
      Log.d("MORE", "Could not load more: " + e.getMessage());
    }

    SearchListResponse response = null;
    try {
      response = query.execute();

    } catch (GoogleJsonResponseException e) {
      switch (e.getStatusCode()) {
        case 403:
          this.KEY = this.context.getString(R.string.api_key2);
          query.setKey(this.KEY);
          try {
            response = query.execute();
          } catch (IOException e1) {
            Log.d("MORE-KEY2", "Could not initialize: " + e1.getMessage());
          }
      }
    } catch (IOException e) {
      Log.d("MORE", "Could not execute more: " + e.getMessage());
      return null;
    }
    List<SearchResult> results = response.getItems();

    ArrayList<YoutubeVideo> items = new ArrayList<YoutubeVideo>();
    for (SearchResult result : results) {
      YoutubeVideo item = new YoutubeVideo();
      item.setTitle(result.getSnippet().getTitle());
      item.setDescription(result.getSnippet().getDescription());
      item.setThumbnailURL(result.getSnippet().getThumbnails().getMedium().getUrl());
      item.setId(result.getId().getVideoId());
      item.setNextPageToken(response.getNextPageToken());
      if (keyword != null) {
        item.setKeyword(keyword);
      }
      try {
        item.setDate(getDateFormat().parse(result.getSnippet().getPublishedAt().toString()));
      } catch (ParseException e) {
        e.printStackTrace();
      }
      item.setChannel_id(this.channel_id);
      items.add(item);
    }
    return items;
  }