teaching machines

CS491: Homework03 xiongchu

November 4, 2011 by . Filed under cs491 mobile, fall 2011, postmortems.

My extra feature is using YouTube’s Data API to extract video information and display them. I actually used their RESTful web service, I made a http request like we did in class and sending the appropriate parameters based on what I want my results to be. Then using the JSON library to parse the response and display the parsed information on a ListActivity.

  

How to request:

https://gdata.youtube.com/feeds/api/standardfeeds/recently_featured

The above is a standard feed, a feed is a single request. In this case, we are requesting the feed for the most recently featured videos, these are the same ones displayed on the YouTube’s main page. There are many parameters fields you can tag with the request, which are helpful to narrower down your feed to exactly what you want. More information here.

https://gdata.youtube.com/feeds/api/videos?
q=android+hello+world&
alt=json&
v=2&
fields=entry(title,author,media:group(media:description,media:player,media:thumbnail))

The above is an example of what I use. The parameters specify what i want. The q parameter, green one, is the search query, what the user is searching for. The alt, red one, specifies the type of result format you want, the default is xml format. The v, blue one, identifies the API version number, version 2 is latest version. The fields, orange one, is probably the most important one when it comes to parsing the results. This parameter is used to specify the exact result set of JSON objects. So: media:description, media:player and media:thumbnail are child objects of media:group. Title, author, and media:group are child objects of entry. Entry is the general JSON array that contains all the information about all the videos. One thing to keep in mind is entry is a child of the feed object, the feed object is the main object that contains all the information about a single request.

Parsing the JSON objects:

// Create JSONObject from json content
jsonObj = new JSONObject(json);

// feed object contains all the information
JSONObject feedObj = jsonObj.getJSONObject("feed");

// entry object contains all result information
JSONArray entryObj = feedObj.getJSONArray("entry");
// Loop through each entry JSONObject and parse information from them
for(int i =0; i < entryObj.length();i++){
	// Get the first video
	JSONObject currentVideo = entryObj.getJSONObject(i);

	// Get title
	String videoTitle="";
	if(currentVideo.getJSONObject("title").has("$t")){
		videoTitle = currentVideo.getJSONObject("title").getString("$t");
	}
....

The above is my starting parsing code. The code is first creating the first JSON object from the response result. Parsing it down, getting the feed object, and then getting the entry array (child of the feed object). And then I loop through the entry array and parse the information I want from it, also each element in the entry array is the information for a single video.

Concurrency Problems:
While handling configurations changes, I ran into a concurrency problem, my ListActivity was displaying previous items after changing the orientation. I couldn’t fix this with retaining and restoring the instance. I had to create a singleton instance of my list of videos. The problem was the previous ListActivity instance was getting updated instead of the current ListActivity instance.