Chee Jit Lim
Chee Jit Lim

Reputation: 1

Implement Search by Keyword on Youtube

I've been struggling on implementing SearchByKeyword for Youtube as I find it hard to relate the Java language used in almost all documentation. I think

https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/Search.java

is the most relevant to what I want but I can't get it to work at all. My confusion:

How do I engage this Search class? For example on a button click? I'm not sure where is the constructor that I can call for and put in the keywords that I want to search for.
For example: In line 90 of the code

String queryTerm = getInputQuery();  

will call
private static String getInputQuery() throws IOException {

    String inputQuery = "";

    System.out.print("Please enter a search term: ");
    BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
    inputQuery = bReader.readLine();

    if (inputQuery.length() < 1) {
        // Use the string "YouTube Developers Live" as a default.
        inputQuery = "YouTube Developers Live";
    }
    return inputQuery;
}  

I want to get my keywords through EditText.getText().toString() and engaging the search by clicking on a Button. Which will return to me a list of results that I can use an ArrayAdapter to be viewed.

How do I do so? Thanks in advance for any aid. I have been searching for guidance for days with no result, there are documentations but I can't translate them into Android. Please help.

Upvotes: 0

Views: 153

Answers (1)

Jo&#227;o Armando
Jo&#227;o Armando

Reputation: 167

Firstly that's your endPoint = https://www.googleapis.com/youtube/v3/search?part=snippet&q=YOURKEYFORSEARCH&type=video&key=YOURAPIKEY;

private void yourQueryToYoutubeAPI(final String keyWord) {

    final  String yourApiKEY = "yourApiKey";
    final String endPoint = "https://www.googleapis.com/youtube/v3/search?part=snippet&q="+ keyWord+"&type=video&key=" + yourApiKEY;
    new Thread(new Runnable() {
        @Override
        public void run() {

            Object jsonResponse = null;

            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpGet request = new HttpGet();

                request.setURI(new URI(endPoint));
                HttpResponse response = httpclient.execute(request);
                InputStream content = response.getEntity().getContent();
                Reader reader = new InputStreamReader(content);
                Gson gson = new Gson();
                jsonResponse = gson.fromJson(reader, HashMap.class);

                //jsonRespose contains your query result you can see result with log for example
                Log.i("jsonResponse", "jsonResponse: " + jsonResponse.toString());

                content.close();


            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }).start();
}`

this will give you an early idea of ​​how to use api, do tests and more tests.

you can find more information on which camps are retrievable here: https://developers.google.com/youtube/v3/docs/search/list?hl=en

extra information: at each query for the search field, it costs 100. you have an initial limit of 1,000,000 you can contact youtube when this limit is close to being reached but I am not sure if you will have to pay for it.

you can find more about the quota limit here: https://developers.google.com/youtube/v3/determine_quota_cost

Upvotes: 1

Related Questions