使用Android Studio上的YouTube Data API v3获取订阅者数量

时间:2017-07-10 15:34:28

标签: java android api youtube

如何在Android Studio上使用YouTube Data API v3获取订阅者数量? 我可以播放视频,但是,无法获得订阅者,谢谢。

2 个答案:

答案 0 :(得分:1)

订阅者计数是enter image description here类的属性。

这是一个允许您搜索频道的工作示例,并返回所有返回结果的订阅者数量。

Array.prototype.join = [].join.bind(null);
[].join([1,2,3,4],"+"); //-> "ABC"

使用输入:H3 Podcast,您将得到结果:

import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTubeRequestInitializer;
import com.google.api.services.youtube.model.*;

import java.io.IOException;
import java.util.*;

class Main {

    public static final String API_KEY = "YOUR KEY HERE";

    public static void main(String[] args) throws Exception {
        YouTube youtube = new YouTube.Builder(
                new NetHttpTransport(),
                new JacksonFactory(),
                new HttpRequestInitializer() {
                        public void initialize(HttpRequest request) throws IOException {
                }
        })
        .setApplicationName("youtube-cmdline-search-sample")
        .setYouTubeRequestInitializer(new YouTubeRequestInitializer(API_KEY))
        .build();


        Scanner input = new Scanner(System.in);

        System.out.println("Search for channel:");
        String queryTerm = input.nextLine();

        YouTube.Search.List search = youtube.search().list("snippet");
        search.setQ(queryTerm);
        search.setType("channel");

        SearchListResponse searchResponse = search.execute();
        List<SearchResult> searchResultList = searchResponse.getItems();
        if (searchResultList != null) {
            for (SearchResult searchResult : searchResultList) {
                String channelId = searchResult.getSnippet().getChannelId();

                YouTube.Channels.List channels = youtube.channels().list("snippet, statistics");
                channels.setId(channelId);

                ChannelListResponse channelResponse = channels.execute();

                for (Channel c : channelResponse.getItems()) {
                    System.out.println("Name: " + c.getSnippet().getTitle());
                    System.out.println("Subs: " + c.getStatistics().getSubscriberCount());
                    System.out.println();
                }
            }
        }

    }

}

我不确定是否有更好的方法,但它确实有效。

答案 1 :(得分:0)

试试这个youtube api

if (l1.V1 == l2.V1)
{
   if (l1.V2 > l2.V2) return 1;
   else return -1;
}
else if (l1.V1 < l2.V1)
          return -1;
else return 1;

希望这是有帮助的;