Reputation: 1
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
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
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