[TUT] Show YouTube Feed for a Users Videos in a ListView ( JSON )

Hi guys, this is a Tutorial Request to show a list of videos that a user has uploaded to their youtube channel.

YouTube has some great API‘s for using the data from their website and you can retrieve this data in a few formats including XML and JSON. This tutorial will retrieve the data using JSON-C. This gives you the information in key value pairs and having the built in JSON library in android makes it easier and cleaner than having to parse a lot of XML.

So the plan is, talk to YouTube ask them for one users uploaded videos. YouTube will then send back a JSON object that we will retrieve the videos from and shove them into a list, sound easy huh.

What we’re going to do:

  • Create an Activity that has a button to initiate the search and a listview to display the results
  • Create a new thread that will talk to youtube and retrieve the JSON
  • Parse the JSON into nice java objects
  • Pass these objects to our ListView and display on the screen

One thing to note is, within our ListView we will be showing a thumbnail of the youtube video and the title, the JSON that YouTube sends back only contains a URL to the thumbnail and not the image itself. Therefore I am going to use code from one of my previous tutorials (UrlImageView) to load the image in an asynchronous manner, this means I’ll just pass the url to the imageview and it’ll go start it’s own thread to load it from the internet and display it when it has, yay.

Ok Here … we .. go.

Lets mix it up a bit and start off with the Manifest, you’ll need to add the Internet permission yes!

AndroidManifest.xml

Lets go ahead and create the MainActivity also, the XML is simple, it contains our Button to go search YouTube and the ListView to display the user uploaded videos when they are returned to us. The ListView is a custom ListView, this makes sense so we can customise it more without the Activity having to care what is going on. Now instead of having to worry about the adapter for the list within your activity we can do it all in our own ListView and just have the Activity call one method to initialise it. It’ll also help later on if you want to save some state when you change orientation.

MainActivity.xml

Next comes the MainActivity class file. Here you want to get a reference to your ListView so you can populate it, you also react to your button click here. When the button is clicked we create a new task and start it running. This task runs on another thread and not the main one. You do not want to do blocking operations on the main thread i.e. calls to internet services. Doing heavy lifting on the main thread is the number one reason for those ‘Activity Not Responding’ errors, bad. We also create a handler here and this is used to receive our data once the task has finished.

MainActivity.java

You then want to create your task, the task takes care of talking to YouTube and making sense of what YouTube sends you back i.e. the JSON-C response. I’ve commented this heavily as it does some interesting things. The one thing I’d note is I do not do any error checking in this tutorial. Yes it catches the errors so you won’t get a forceclose but if you start this task from your activity then sit there waiting for it to finish and reply, if it gets an error .. it won’t and your activity will need to react to that. Thats just one thing to note if you want to expand on this code.

GetYouTubeUserVideosTask.java

When the task is complete your handler is notified and the message the handler gets has a bundle attached (just like when you attach extras to an Intent). From this bundle we can retrieve the list of videos that YouTube has told us the user we searched for uploaded, in this case ‘blundellp’ me!
We’re storing each YouTube video in an object of our own called Video, this has a title, url, and thumbnail url. If you notice I like to make my objects immutable, this means you set all the fields on them in the constructor and once that has happened the fields cannot change and you can only retrieve what they are. This helps when your stepping through code and you then know crazy things can’t happen after instantiation. This isn’t always possible in practice, but it is here!

Video.java

The videos are then stored in an ArrayList, which we want to pass back to our activity in the bundle. I know you remembered the List interface doesn’t implement serialisable in Java so we then wrap that list in another object which we are calling a Library, for good measure we’ll store the username we searched for in the Library as well. Again this object is immutable.

Library.java

Finally in the Activity when this Library is retrieved from the task it is passed to our custom listview that uses a custom adapter to read the list, take the YouTube video title and thumbnail url and populate one list row for each video.

VideoListView.java

The VideoAdapter uses the UrlImageView to load the thumbnails. Apart from that it is a simple view adapter.

VideoAdaper.java

That’s it! The last two files here are just for completeness, I have a custom Log class so I can control the log message that I output. Also a utility class to convert the InputStream that is retrieved from talking to YouTube into a string (because it’s actually just JSON-C).

Log.java

StreamUtils.java

That’s Definitely it! You now should be able to query YouTube to retrieve a JSON object, parse this JSON object into a domain object of your own, pass a list of domain objects to a view and have it display the user uploaded videos on your phone screen, magic!

Here’s the Search User Feed YouTube Tutorial Project Eclipse Source Files <--- It is also mirrored on GitHub, feel free to make changes: https://github.com/blundell/YouTubeUserFeed

I really really really recommend following a similar package structure to what is outlines in this project, it makes your code so much more maintainable in the long run and if you follow any of my other tutorials you’ll recognise the pattern and learn that, that much quicker 😉

P.s. that is a video on my YouTube channel of me attempting to grow a moustache! It was for charity, so if you could up vote it that’d be a great help.

89 thoughts on “[TUT] Show YouTube Feed for a Users Videos in a ListView ( JSON )

  1. Great tutorial! I have spent countless hours trying to modifying your code but to no success. Since the release of the new youtube API i wanted to incorporate some of the features into your code. Mainly when you click on a video it will play in your app instead of prompting out of the app.

    Could you create another tutorial on how to implement new youtube API?

  2. i really liked your tutorial a lot i am stuck at one place could you please help me i need array of youtube urls in my main activity could you help me please

  3. Hi there, great tutorial!

    I’ve been trying to add it to my app so i can get a base on which to work but im struggling.

    I got the code errorless, and when i run it, on clicking the button on the activity_main.xml, i get a java.lang.IllegalStateException (could not execute method of activity) at android.view.View$1.onClick and the list goes on, but starts there..

    Could you nudge me in the right direction ?

    Thanks.

    1. Have you declared the onClick method in your layout XML file? And does this match exactly to the name of the method in your Activity?

    2. If you want i could send you the whole error somewhere (e-mail, pastebin?)

    3. I think i got it, but shouldnt the app be runnable on Android 4+ ?

      Even on your version, if you put “android:targetSdkVersion=”16″ ” in the manifest, when you click the Refresh button, the app shuts down.

      It throws a “networkOnMainThreadException”.

    4. Yes, well you already done it, i didn’t change it:

      android:onClick=”getUserYouTubeFeed”

      public void getUserYouTubeFeed(View v){

Comments are closed.