app/src/main/java/com/smedic/tubtub/YouTubeSearch.java
public void searchVideos(final String keywords) {

        new Thread() {
            public void run() {
                try {
                    youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
                        @Override
                        public void initialize(HttpRequest request) throws IOException {

                        }
                    }).setApplicationName(appName).build();

                    YouTube.Search.List searchList;
                    YouTube.Videos.List videosList;

                    searchList = youtube.search().list("id,snippet");
                    searchList.setKey(Config.YOUTUBE_API_KEY);
                    searchList.setType("video"); //TODO ADD PLAYLISTS SEARCH
                    searchList.setMaxResults(Config.NUMBER_OF_VIDEOS_RETURNED);
                    searchList.setFields("items(id/videoId,snippet/title,snippet/thumbnails/default/url)");

                    videosList = youtube.videos().list("id,contentDetails,statistics");
                    videosList.setKey(Config.YOUTUBE_API_KEY);
                    videosList.setFields("items(contentDetails/duration,statistics/viewCount)");

                    //search
                    searchList.setQ(keywords);
                    SearchListResponse searchListResponse = searchList.execute();
                    List<SearchResult> searchResults = searchListResponse.getItems();

                    //save all ids from searchList list in order to find video list
                    StringBuilder contentDetails = new StringBuilder();

                    int ii = 0;
                    for (SearchResult result : searchResults) {
                        contentDetails.append(result.getId().getVideoId());
                        if (ii < 49)
                            contentDetails.append(",");
                        ii++;
                    }

                    //find video list
                    videosList.setId(contentDetails.toString());
                    VideoListResponse resp = videosList.execute();
                    List<Video> videoResults = resp.getItems();
                    //make items for displaying in listView
                    ArrayList<YouTubeVideo> items = new ArrayList<>();
                    for (int i = 0; i < searchResults.size(); i++) {
                        YouTubeVideo item = new YouTubeVideo();
                        //searchList list info
                        item.setTitle(searchResults.get(i).getSnippet().getTitle());
                        item.setThumbnailURL(searchResults.get(i).getSnippet().getThumbnails().getDefault().getUrl());
                        item.setId(searchResults.get(i).getId().getVideoId());
                        //video info
                        if (videoResults.get(i) != null) {
                            BigInteger viewsNumber = videoResults.get(i).getStatistics().getViewCount();
                            String viewsFormatted = NumberFormat.getIntegerInstance().format(viewsNumber) + " views";
                            item.setViewCount(viewsFormatted);
                            String isoTime = videoResults.get(i).getContentDetails().getDuration();
                            String time = Utils.convertISO8601DurationToNormalTime(isoTime);
                            item.setDuration(time);
                        } else {
                            item.setDuration("NA");
                        }

                        //add to the list
                        items.add(item);
                    }

                    youTubeVideosReceiver.onVideosReceived(items);

                } catch (IOException e) {
                    Log.e(TAG, "Could not initialize: " + e);
                    e.printStackTrace();
                    return;
                }
            }
        }.start();
    }
/**
     * Search videos for a specific query
     *
     * @param keywords - query
     */
Search videos for a specific query
@param keywords - query
Search videos for a specific query @param keywords - query
public void searchVideos(final String keywords) {
        new Thread() {
            public void run() {
                try {
                    youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
                        @Override
                        public void initialize(HttpRequest request) throws IOException {
                        }
                    }).setApplicationName(appName).build();
                    YouTube.Search.List searchList;
                    YouTube.Videos.List videosList;
                    searchList = youtube.search().list("id,snippet");
                    searchList.setKey(Config.YOUTUBE_API_KEY);
                    searchList.setType("video"); 
                    searchList.setMaxResults(Config.NUMBER_OF_VIDEOS_RETURNED);
                    searchList.setFields("items(id/videoId,snippet/title,snippet/thumbnails/default/url)");
                    videosList = youtube.videos().list("id,contentDetails,statistics");
                    videosList.setKey(Config.YOUTUBE_API_KEY);
                    videosList.setFields("items(contentDetails/duration,statistics/viewCount)");
                    searchList.setQ(keywords);
                    SearchListResponse searchListResponse = searchList.execute();
                    List<SearchResult> searchResults = searchListResponse.getItems();
                    StringBuilder contentDetails = new StringBuilder();
                    int ii = 0;
                    for (SearchResult result : searchResults) {
                        contentDetails.append(result.getId().getVideoId());
                        if (ii < 49)
                            contentDetails.append(",");
                        ii++;
                    }
                    videosList.setId(contentDetails.toString());
                    VideoListResponse resp = videosList.execute();
                    List<Video> videoResults = resp.getItems();
                    ArrayList<YouTubeVideo> items = new ArrayList<>();
                    for (int i = 0; i < searchResults.size(); i++) {
                        YouTubeVideo item = new YouTubeVideo();
                        List list info
                        item.setTitle(searchResults.get(i).getSnippet().getTitle());
                        item.setThumbnailURL(searchResults.get(i).getSnippet().getThumbnails().getDefault().getUrl());
                        item.setId(searchResults.get(i).getId().getVideoId());
                        if (videoResults.get(i) != null) {
                            BigInteger viewsNumber = videoResults.get(i).getStatistics().getViewCount();
                            String viewsFormatted = NumberFormat.getIntegerInstance().format(viewsNumber) + " views";
                            item.setViewCount(viewsFormatted);
                            String isoTime = videoResults.get(i).getContentDetails().getDuration();
                            String time = Utils.convertISO8601DurationToNormalTime(isoTime);
                            item.setDuration(time);
                        } else {
                            item.setDuration("NA");
                        }
                        items.add(item);
                    }
                    youTubeVideosReceiver.onVideosReceived(items);
                } catch (IOException e) {
                    Log.e(TAG, "Could not initialize: " + e);
                    e.printStackTrace();
                    return;
                }
            }
        }.start();
    }
public void searchVideos ( final String keywords ) { new Thread ( ) { public void run ( ) { try { youtube = new YouTube . Builder ( new NetHttpTransport ( ) , new JacksonFactory ( ) , new HttpRequestInitializer ( ) { @ Override public void initialize ( HttpRequest request ) throws IOException { } } ) . setApplicationName ( appName ) . build ( ) ; YouTube . Search . List searchList ; YouTube . Videos . List videosList ; searchList = youtube . search ( ) . list ( "id,snippet" ) ; searchList . setKey ( Config . YOUTUBE_API_KEY ) ; searchList . setType ( "video" ) ; searchList . setMaxResults ( Config . NUMBER_OF_VIDEOS_RETURNED ) ; searchList . setFields ( "items(id/videoId,snippet/title,snippet/thumbnails/default/url)" ) ; videosList = youtube . videos ( ) . list ( "id,contentDetails,statistics" ) ; videosList . setKey ( Config . YOUTUBE_API_KEY ) ; videosList . setFields ( "items(contentDetails/duration,statistics/viewCount)" ) ; searchList . setQ ( keywords ) ; SearchListResponse searchListResponse = searchList . execute ( ) ; List < SearchResult > searchResults = searchListResponse . getItems ( ) ; StringBuilder contentDetails = new StringBuilder ( ) ; int ii = 0 ; for ( SearchResult result : searchResults ) { contentDetails . append ( result . getId ( ) . getVideoId ( ) ) ; if ( ii < 49 ) contentDetails . append ( "," ) ; ii ++ ; } videosList . setId ( contentDetails . toString ( ) ) ; VideoListResponse resp = videosList . execute ( ) ; List < Video > videoResults = resp . getItems ( ) ; ArrayList < YouTubeVideo > items = new ArrayList < > ( ) ; for ( int i = 0 ; i < searchResults . size ( ) ; i ++ ) { YouTubeVideo item = new YouTubeVideo ( ) ; item . setTitle ( searchResults . get ( i ) . getSnippet ( ) . getTitle ( ) ) ; item . setThumbnailURL ( searchResults . get ( i ) . getSnippet ( ) . getThumbnails ( ) . getDefault ( ) . getUrl ( ) ) ; item . setId ( searchResults . get ( i ) . getId ( ) . getVideoId ( ) ) ; if ( videoResults . get ( i ) != null ) { BigInteger viewsNumber = videoResults . get ( i ) . getStatistics ( ) . getViewCount ( ) ; String viewsFormatted = NumberFormat . getIntegerInstance ( ) . format ( viewsNumber ) + " views" ; item . setViewCount ( viewsFormatted ) ; String isoTime = videoResults . get ( i ) . getContentDetails ( ) . getDuration ( ) ; String time = Utils . convertISO8601DurationToNormalTime ( isoTime ) ; item . setDuration ( time ) ; } else { item . setDuration ( "NA" ) ; } items . add ( item ) ; } youTubeVideosReceiver . onVideosReceived ( items ) ; } catch ( IOException e ) { Log . e ( TAG , "Could not initialize: " + e ) ; e . printStackTrace ( ) ; return ; } } } . start ( ) ; }
Search videos for a specific query
 @param keywords - query
Search videos for a specific query @param keywords - query
//TODO ADD PLAYLISTS SEARCH //search //save all ids from searchList list in order to find video list //find video list //make items for displaying in listView //searchList list info //video info //add to the list