我试图插入到我的应用程序的订阅者和观看从YouTube频道的数量。我怎么做呢?
我已经设置了YouTube API并获得了我的API密钥。
我也有URL给我下面的代码。
我怎么把这个:
{
"kind": "youtube#channelListResponse",
"etag": "REMOVED",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "REMOVED",
"id": "REMOVED",
"statistics": {
"viewCount": "13398211",
"commentCount": "28",
"subscriberCount": "182758",
"hiddenSubscriberCount": false,
"videoCount": "84"
}
}
]
}
这是您需要的请求:
YouTube youTube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), null).setApplicationName("yourAppName").build();
YouTube.Channels.List channelListRequest = youTube.channels().list("statistics");
channelListRequest.setKey(DEVELOPER_KEY);
channelListRequest.setId(channelID);
channelListRequest.setFields("items/statistics(viewCount, subscriberCount)");
ChannelListResponse channelListResponse = channelListRequest.execute();
Channel channel = channelListResponse.getItems().get(0);
此时,您已经拥有了包含所需数据的Channel对象。你可以很容易地从这个对象中获得数据。
BigInteger viewCount = channel .getStatistics().getViewCount();
BigInteger subscriberCount = channel.getStatistics().getSubscriberCount();