title: Using YouTube Data Api in Android
body: <p>Am trying to use YouTube Data Api in my android app. When i try to run it as a java project, it runs fine without any errors. But when i try to do the same thing in my android application , the application force closes. Here is the output from logcat and my code.</p>
  
  <p>OUTPUT FROM LOGCAT</p>
  
  <pre><code>01-05 00:13:17.198: E/AndroidRuntime(627): FATAL EXCEPTION: main
  01-05 00:13:17.198: E/AndroidRuntime(627): java.lang.ExceptionInInitializerError
  01-05 00:13:17.198: E/AndroidRuntime(627):  at com.example.allinonedata.YouTubeManager.retrieveVideos(YouTubeManager.java:29)
  ...
  01-05 00:13:17.198: E/AndroidRuntime(627): Caused by: java.lang.NoClassDefFoundError: com.google.gdata.data.media.MediaSource
  </code></pre>
  
  <p>HERE IS THE CODE
  mainactivity.java</p>
  
  <pre><code>  package com.example.allinonedata;
   import java.util.List;
  
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
  
     public class MainActivity extends Activity {
  String clientID = "JavaCodeGeeks";
  String textQuery = "nexus 4";
  int maxResults = 1;
  boolean filter = true;
  int timeout = 2000;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      YouTubeManager ym = new YouTubeManager(clientID);
      try{
      List&lt;YouTubeVideo&gt; videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout);
  
      for (YouTubeVideo youtubeVideo : videos) {
          System.out.println(youtubeVideo.getWebPlayerUrl());
  
          System.out.println("Thumbnails");
          for (String thumbnail : youtubeVideo.getThumbnails()) {
              System.out.println("\t" + thumbnail);
          }
          System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl());
          System.out.println("**************************************************");
      }
      }
      catch(Exception e)
      {
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>youtubemanager.java</p>
  
  <pre><code> package com.example.allinonedata;
  
   import java.net.URL;
   import java.util.LinkedList;
   import java.util.List;
  
   import com.google.gdata.client.youtube.YouTubeQuery;
   import com.google.gdata.client.youtube.YouTubeService;
   import com.google.gdata.data.media.mediarss.MediaThumbnail;
   import com.google.gdata.data.youtube.VideoEntry;
   import com.google.gdata.data.youtube.VideoFeed;
   import com.google.gdata.data.youtube.YouTubeMediaContent;
   import com.google.gdata.data.youtube.YouTubeMediaGroup;
  
   public class YouTubeManager {
  
  private static final String YOUTUBE_URL = "http://gdata.youtube.com/feeds/api/videos";
  private static final String YOUTUBE_EMBEDDED_URL = "http://www.youtube.com/v/";
  
  private String clientID;
  
  public YouTubeManager(String clientID) {
      this.clientID = clientID;
  }
  
  public List&lt;YouTubeVideo&gt; retrieveVideos(String textQuery, int maxResults, 
          boolean filter, int timeout) throws Exception {
  
      YouTubeService service = new YouTubeService(clientID);
      service.setConnectTimeout(timeout); // millis
      YouTubeQuery query = new YouTubeQuery(new URL(YOUTUBE_URL));
  
      query.setOrderBy(YouTubeQuery.OrderBy.RELEVANCE);
      query.setFullTextQuery(textQuery);
      query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);
      query.setMaxResults(maxResults);
  
      VideoFeed videoFeed = service.query(query, VideoFeed.class);        
      List&lt;VideoEntry&gt; videos = videoFeed.getEntries();
  
      return convertVideos(videos);
  
  }
  
  private List&lt;YouTubeVideo&gt; convertVideos(List&lt;VideoEntry&gt; videos) {
  
      List&lt;YouTubeVideo&gt; youtubeVideosList = new LinkedList&lt;YouTubeVideo&gt;();
  
      for (VideoEntry videoEntry : videos) {
  
          YouTubeVideo ytv = new YouTubeVideo();
  
          YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
          String webPlayerUrl = mediaGroup.getPlayer().getUrl();
          ytv.setWebPlayerUrl(webPlayerUrl);
  
          String query = "?v=";
          int index = webPlayerUrl.indexOf(query);
          String embeddedWebPlayerUrl = webPlayerUrl.substring(index+query.length());
          embeddedWebPlayerUrl = YOUTUBE_EMBEDDED_URL + embeddedWebPlayerUrl;
          ytv.setEmbeddedWebPlayerUrl(embeddedWebPlayerUrl);
  
          List&lt;String&gt; thumbnails = new LinkedList&lt;String&gt;();
          for (MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) {
              thumbnails.add(mediaThumbnail.getUrl());
          }           
          ytv.setThumbnails(thumbnails);
  
          List&lt;YouTubeMedia&gt; medias = new LinkedList&lt;YouTubeMedia&gt;();
          for (YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents()) {
              medias.add(new YouTubeMedia(mediaContent.getUrl(), mediaContent.getType()));
          }
          ytv.setMedias(medias);
  
          youtubeVideosList.add(ytv);
  
      }
  
      return youtubeVideosList;
  
  }
  </code></pre>
  
  <p>}</p>