Android Open Source - Demo-YouTuBe-Android Search






From Project

Back to project page Demo-YouTuBe-Android.

License

The source code is released under:

Apache License

If you think the Android project Demo-YouTuBe-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lqg.youtube.support.http;
/* w  w  w . ja  v a  2  s  .  c  om*/
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.SearchListResponse;
import com.lqg.youtube.support.util.LogUtil;
import com.lqg.youtube.support.util.SearchSetting;

import java.io.IOException;

public class Search {

    public static final HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();

    public static final JsonFactory JSON_FACTORY = new AndroidJsonFactory();

    public static String apiKey = "YOURAPIKEY";

    private static YouTube youtube;

    static {
        youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("YOURAPPLICATIONNAME").build();
    }

    public static SearchListResponse search(String queryTerm) {

        try {
            YouTube.Search.List search = youtube.search().list("id,snippet")
                    .setKey(apiKey)
                    .setQ(queryTerm)
                    .setType("video")
                    .setFields("items(id(videoId),snippet(title,description,thumbnails/default/url))")
                    .setMaxResults(Long.valueOf(SearchSetting.getInstance().getMaxResults()))
                    .setOrder(SearchSetting.getInstance().getOrder())
                    .setSafeSearch(SearchSetting.getInstance().getSafeSearch())
                    .setVideoDefinition(SearchSetting.getInstance().getVideoDefinition())
                    .setVideoDuration(SearchSetting.getInstance().getVideoDuration())
                    .setVideoType(SearchSetting.getInstance().getVideoType());
            return search.execute();
        } catch (GoogleJsonResponseException e) {
            LogUtil.d("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        } catch (IOException e) {
            LogUtil.d("There was an IO error: " + e.getCause() + " : " + e.getMessage());
        } catch (Throwable t) {
            LogUtil.d(t);
        }

        return null;
    }

}




Java Source Code List

com.lqg.youtube.support.GlobalApplication.java
com.lqg.youtube.support.http.Search.java
com.lqg.youtube.support.player.Format.java
com.lqg.youtube.support.player.UrlParser.java
com.lqg.youtube.support.player.VideoId.java
com.lqg.youtube.support.player.VideoStream.java
com.lqg.youtube.support.util.ImageLoaderUtil.java
com.lqg.youtube.support.util.LogUtil.java
com.lqg.youtube.support.util.SearchSetting.java
com.lqg.youtube.ui.play.PlayVideoUsingVideoViewActivity.java
com.lqg.youtube.ui.play.PlayVideoUsingYouTuBeActivity.java
com.lqg.youtube.ui.search.MainActivity.java
com.lqg.youtube.ui.search.SearchFragment.java
com.lqg.youtube.ui.search.SearchResultAdapter.java
com.lqg.youtube.ui.search.SearchSetingFragment.java