1

I'm trying to insert into my app the number of subscribers and views from a channel on YouTube. How do I do that?

I've already setup the YouTube API and gotten my API Key.

I also have the URL that gives me the code below.

How do I turn this:

{
 "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"
   }
  }
 ]
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • This should help you parse JSON string, and pass the value to your constructor http://stackoverflow.com/questions/13196234/simple-parse-json-from-url-on-android – Verma Aug 17 '15 at 17:49

1 Answers1

1

this is the request you need:

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

at this point you have the Channel object which contains the data you need. You can easly get the data from this object.

BigInteger viewCount = channel .getStatistics().getViewCount();
BigInteger subscriberCount = channel.getStatistics().getSubscriberCount();
Pierfrancesco Soffritti
  • 1,678
  • 1
  • 18
  • 22