Many a times, we are stuck in a condition where we want to read data from YouTube, i.e, we want to browse YouTube but not upload data. For this, YouTube itself provides gdata library for Java developers to fetch as well as upload videos on YouTube.

Youtube Data Reading

Gdata integration with Android is available on google developers site, but we want to keep resources size limited. So let’s start with YouTube.

To get the list of categories to browse YouTube, visit www.youtube.com and go to browse. Another way is to visit gdata.youtube.com . There you will find categories and their keywords.

Category Keyword
Autos and Vehicle Autos
Comedy Comedy
Education Education
Entertainment Entertainment
Film and animation Film
Howto and Style Howto
Music Music
News and Politics News
People and Blog People
Pets and Animals Animals
Science and Technology Tech
Sports Sports
Travel and Events Travel

There are some standard feeds also available :

Feed type Keyword
Most recent most_recent
Most viewed most_viewed
Top rated top_rated
Most discussed most_discussed
Top favorites top_favorites
Recently featured recently_featured
Most Responded most_responded

Now, how do we use these keywords and get our data from YouTube?
We wil call a GET method using web services from our code which gives some data in response and we utilize it as per our requirement.
Generally, we work with JSON and JSON data is very easy to read and parse.

So can we get JSON data from gdata YouTube by calling a url from GET method?
>
The answer is yes.

URL pattern :

https://gdata.youtube.com/feeds/api/videos

+
/-/Category_Keyword
+
?alt=jsonc&start-index=
+
“Whatever your start number from 1-998”
+
&max-results=
+
“Max results 1-50”
+
&v=2
Now by merging this, we get a url like this

https://gdata.youtube.com/feeds/api/videos/-/Music?alt=jsonc&start-index=1&max-results=50&v=2

(This is for Music Category)

Once you run this on your browser, you will get JSON data. Use any of the JSON data parsers to view it.

You can see items –> 50 , which is the max-results we put in the query.
On expanding the items, you will find the following result.

You can use Gson library on any other library to parse this JSON data and make an object from it.</

For standard feeds

http://gdata.youtube.com/feeds/api/standardfeeds/
+
most_responded
+
?alt=jsonc&start-index=
+
“Any number 1-999”
+
&max-results=
+
“any number 1-50”
+
&v=2

Merged URL looks like this

http://gdata.youtube.com/feeds/api/standardfeeds/most_responded?alt=jsonc&start-index=1&max-results=50&v=2

And on opening this URL on your browser, you get a similar JSON response which you can parse.

Now some AND and OR operations :

You cannot perform And and Or operations in standard feeds, but you can in category browsing.

For And operations.

https://gdata.youtube.com/feeds/api/videos
+
/-/Category_Keyword_1/Category_Keyword_2/Category_Keyword_n
+
?alt=jsonc&start-index=
+
“Whatever your start number from 1-998”
+
&max-results=
+
“Max results 1-50”
+
&v=2

https://gdata.youtube.com/feeds/api/videos/-/Music/Comdey?alt=jsonc&start-index=1&max-results=50&v=2

For Or operation

https://gdata.youtube.com/feeds/api/videos
+
/-/Category_Keyword_1|Category_Keyword_2|Category_Keyword_n
+
?alt=jsonc&start-index=
+
“Whatever your start number from 1-998”
+
&max-results=
+
“Max results 1-50”standard
+
&v=2

https://gdata.youtube.com/feeds/api/videos/-/Music|Comdey?alt=jsonc&start-index=1&max-results=50&v=2
(For Music or Comedy)

You can also merge standard feeds with category & category “AND” operation. Category “OR” operation looks something like this :
&nbsp

Standard feeds with category

http://gdata.youtube.com/feeds/api/standardfeeds/most_responded/-/Music?alt=jsonc&start-index=1&max-results=50&v=2

Standard feeds with Category And operation

http://gdata.youtube.com/feeds/api/standardfeeds/most_responded/-/Music/Comedy?alt=jsonc&start-index=1&max-results=50&v=2

Standard feeds with Category Or operation

http://gdata.youtube.com/feeds/api/standardfeeds/most_responded/-/Music|Comedy?alt=jsonc&start-index=1&max-results=50&v=2

Now last but not the least – Searching

For generic search, simply add some parameter

http://gdata.youtube.com/feeds/api/videos?q=”SEARCH_STRING”?alt=jsonc&start-index=1&max-results=50&v=2

For searching in particular category

http://gdata.youtube.com/feeds/api/videos/-/Comedy?q=”SEARCH_STRING”?alt=jsonc&start-index=1&max-results=50&v=2

You can also merge AND/OR operations, standard feeds or any possible combination with the search parameter.
http://gdata.youtube.com/feeds/api/standardfeeds/most_responded/-/Music?q=”SEARCH_STRING”?alt=jsonc&start-index=1&max-results=50&v=2

Remember ?alt=json is important for JSON and start-index=”n”&max-results=”m” will be avoided and &v=2 is the JSON version you must include.

One more thing, for browse all video or search all videos, simply use :

http://gdata.youtube.com/feeds/api/videos
+
?alt=jsonc&start-index=1&max-results=50&v=2

http://gdata.youtube.com/feeds/api/videos?alt=jsonc&start-index=1&max-results=50&v=2

YouTube use filters in request URL for browsing and searching YouTube video and thus many times we didn’t find similar results in by browsing programmatically. for now remember one tag

“orderby”

add “orderby=relevance” in your request your, don’t forget to follow URL pattern, i.e;
after absolute URL
“?orderby=relevance”
after some other request parameter
“&orderby=relevance”

now your URL look like this —

https://gdata.youtube.com/feeds/api/videos/-/Music?alt=jsonc&start-index=1&max-results=50&v=2&orderby=relevance

public class YouTubeModelForJson {

	private String apiVersion;
	private YouTubeData data;
	private YouTubeError error;

	public String getApiVersion() {
		return apiVersion;
	}

	public void setApiVersion(String apiVersion) {
		this.apiVersion = apiVersion;
	}

	public YouTubeData getData() {
		return data;
	}

	public void setData(YouTubeData data) {
		this.data = data;
	}

	public YouTubeError getError() {
		return error;
	}

	public void setError(YouTubeError error) {
		this.error = error;
	}
}
public class YouTubeData {

	private String updated;
	private String totalItems;
	private String startIndex;
	private String itemsPerPage;
	private ArrayList<YouTubeItems> items;

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
	}

	public String getTotalItems() {
		return totalItems;
	}

	public void setTotalItems(String totalItems) {
		this.totalItems = totalItems;
	}

	public String getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(String startIndex) {
		this.startIndex = startIndex;
	}

	public String getItemsPerPage() {
		return itemsPerPage;
	}

	public void setItemsPerPage(String itemsPerPage) {
		this.itemsPerPage = itemsPerPage;
	}

	public ArrayList<YouTubeItems> getItems() {
		return items;
	}

	public void setItems(ArrayList<YouTubeItems> items) {
		this.items = items;
	}

}

public class YouTubeItems {

	private String id;
	private String uploaded;
	private String updated;
	private String uploader;
	private String category;
	private String title;
	private String description;
	private YouTubeContent content;
	private YouTubeThumbnails thumbnail;
	private String duration;
	private String aspectRatio;
	private String rating;
	private String likeCount;
	private String ratingCount;
	private String viewCount;
	private String favoriteCount;
	private String commentCount;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUploaded() {
		return uploaded;
	}

	public void setUploaded(String uploaded) {
		this.uploaded = uploaded;
	}

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
	}

	public String getUploader() {
		return uploader;
	}

	public void setUploader(String uploader) {
		this.uploader = uploader;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public YouTubeContent getContent() {
		return content;
	}

	public void setContent(YouTubeContent content) {
		this.content = content;
	}

	public YouTubeThumbnails getThumbnail() {
		return thumbnail;
	}

	public void setThumbnail(YouTubeThumbnails thumbnail) {
		this.thumbnail = thumbnail;
	}

	public String getDuration() {
		return duration;
	}

	public void setDuration(String duration) {
		this.duration = duration;
	}

	public String getAspectRatio() {
		return aspectRatio;
	}

	public void setAspectRatio(String aspectRatio) {
		this.aspectRatio = aspectRatio;
	}

	public String getRating() {
		return rating;
	}

	public void setRating(String rating) {
		this.rating = rating;
	}

	public String getLikeCount() {
		return likeCount;
	}

	public void setLikeCount(String likeCount) {
		this.likeCount = likeCount;
	}

	public String getRatingCount() {
		return ratingCount;
	}

	public void setRatingCount(String ratingCount) {
		this.ratingCount = ratingCount;
	}

	public String getViewCount() {
		return viewCount;
	}

	public void setViewCount(String viewCount) {
		this.viewCount = viewCount;
	}

	public String getFavoriteCount() {
		return favoriteCount;
	}

	public void setFavoriteCount(String favoriteCount) {
		this.favoriteCount = favoriteCount;
	}

	public String getCommentCount() {
		return commentCount;
	}

	public void setCommentCount(String commentCount) {
		this.commentCount = commentCount;
	}

}
public class YouTubeContent {
	@SerializedName("5")
	private String link1;
	@SerializedName("1")
	private String link2;
	@SerializedName("6")
	private String link3;

	public String getLink1() {
		return link1;
	}

	public void setLink1(String link1) {
		this.link1 = link1;
	}

	public String getLink2() {
		return link2;
	}

	public void setLink2(String link2) {
		this.link2 = link2;
	}

	public String getLink3() {
		return link3;
	}

	public void setLink3(String link3) {
		this.link3 = link3;
	}
}
public class YouTubeThumbnails {

	private String sqDefault;
	private String hqDefault;

	public String getSqDefault() {
		return sqDefault;
	}

	public void setSqDefault(String sqDefault) {
		this.sqDefault = sqDefault;
	}

	public String getHqDefault() {
		return hqDefault;
	}

	public void setHqDefault(String hqDefault) {
		this.hqDefault = hqDefault;
	}
}

public class YouTubeError {
	private String code;
	private String message;
	private ArrayList<YouTubeErrors> errors;

	public String getCode400() {
		return code;
	}

	public void setCode400(String code400) {
		this.code = code400;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ArrayList<YouTubeErrors> getErrors() {
		return errors;
	}

	public void setErrors(ArrayList<YouTubeErrors> errors) {
		this.errors = errors;
	}
}

public class YouTubeErrors {
	private String domain;
	private String code;
	private String internalReason;

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getInternalReason() {
		return internalReason;
	}

	public void setInternalReason(String internalReason) {
		this.internalReason = internalReason;
	}
}

What you can do using this Blog —

1. Read YouTube data without YouTube account.

2. You can keep your resource size limited as no need to add gdata library in your project.

3. Data received in JSON format which easy to parse.

4. Http get request allows you to read YouTube data.

Click here to download YouTube data reading sample