Hello everyone, today I am providing a code for those android developers who wants to fetch the list of Youtube videos on their application rather than upload the same on the application and making it heavy and slow.
For API v3(The YouTube Data API (v2) has been officially deprecated as of March 4, 2014. ):
// Get a httpclient to talk to the internet
HttpClient client = new DefaultHttpClient();
// Perform a GET request to YouTube for a JSON list of all the
// videos by a specific user
String final_username = URLEncoder.encode(username, "UTF-8");
// HttpUriRequest request = new
// HttpGet("https://gdata.youtube.com/feeds/api/videos?q="+final_username+"&v=2&alt=jsonc");
HttpUriRequest request = new HttpGet("https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + final_username + "&type=video&key=YOUR_API_KEY");
// Get the response that YouTube sends back
HttpResponse response = client.execute(request);
// Convert this response into a readable string
String jsonString = globalUtills.convertToString(response.getEntity().getContent());
// Create a JSON object that we can use from the String
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("items");
// Create a list to store are videos in
List<Video> videos = new ArrayList<Video>();
// Loop round our JSON list of videos creating Video objects to use
// within our app
for( int i = 0; i < jsonArray.length(); i++ )
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
// The title of the video
String V_id = jsonObject.getJSONObject("id").getString("videoId");
String title = jsonObject.getJSONObject("snippet").getString("title");
JSONObject thumbUrlJ = jsonObject.getJSONObject("snippet").getJSONObject("thumbnails");
String uRL_ThumbString = thumbUrlJ.getJSONObject("default").getString("url");
}
For API v2:
try {
// Get a httpclient to talk to the internet
HttpClient client = new DefaultHttpClient();
// Perform a GET request to YouTube for a JSON list of all the videos by a specific user
String final_username = URLEncoder.encode(username, "UTF-8");
HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?q="+SEARCH_TEXT+"&v=2&alt=jsonc");
// Get the response that YouTube sends back
HttpResponse response = client.execute(request);
// Convert this response into a readable string
String jsonString = convertToString(response.getEntity().getContent()); //use convertToString method to convert response to string
// Create a JSON object that we can use from the String
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
// Create a list to store are videos in
List<String> videos = new ArrayList<String>();
// Loop round our JSON list of videos creating Video objects to use within our app
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// The title of the video
String title = jsonObject.getString("title");
// The url link back to YouTube, this checks if it has a mobile url
// if it doesnt it gets the standard url
String url;
try {
url = jsonObject.getJSONObject("player").getString("mobile");
} catch (JSONException ignore) {
url = jsonObject.getJSONObject("player").getString("default");
}
// A url to the thumbnail image of the video
// We will use this later to get an image using a Custom ImageView
// Found here http://blog.blundell-apps.com/imageview-with-loading-spinner/
String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
// **add these URL's in a arrayList.
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
} catch (JSONException e) {
}
catch (Exception e) {
e.printStackTrace();
}
public static String convertToString(InputStream inputStream)
throws IOException {
if (inputStream != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"), 1024);
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
inputStream.close();
}
return writer.toString();
} else {
return "";
}
}
For API v3(The YouTube Data API (v2) has been officially deprecated as of March 4, 2014. ):
// Get a httpclient to talk to the internet
HttpClient client = new DefaultHttpClient();
// Perform a GET request to YouTube for a JSON list of all the
// videos by a specific user
String final_username = URLEncoder.encode(username, "UTF-8");
// HttpUriRequest request = new
// HttpGet("https://gdata.youtube.com/feeds/api/videos?q="+final_username+"&v=2&alt=jsonc");
HttpUriRequest request = new HttpGet("https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + final_username + "&type=video&key=YOUR_API_KEY");
// Get the response that YouTube sends back
HttpResponse response = client.execute(request);
// Convert this response into a readable string
String jsonString = globalUtills.convertToString(response.getEntity().getContent());
// Create a JSON object that we can use from the String
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("items");
// Create a list to store are videos in
List<Video> videos = new ArrayList<Video>();
// Loop round our JSON list of videos creating Video objects to use
// within our app
for( int i = 0; i < jsonArray.length(); i++ )
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
// The title of the video
String V_id = jsonObject.getJSONObject("id").getString("videoId");
String title = jsonObject.getJSONObject("snippet").getString("title");
JSONObject thumbUrlJ = jsonObject.getJSONObject("snippet").getJSONObject("thumbnails");
String uRL_ThumbString = thumbUrlJ.getJSONObject("default").getString("url");
}
For API v2:
try {
// Get a httpclient to talk to the internet
HttpClient client = new DefaultHttpClient();
// Perform a GET request to YouTube for a JSON list of all the videos by a specific user
String final_username = URLEncoder.encode(username, "UTF-8");
HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?q="+SEARCH_TEXT+"&v=2&alt=jsonc");
// Get the response that YouTube sends back
HttpResponse response = client.execute(request);
// Convert this response into a readable string
String jsonString = convertToString(response.getEntity().getContent()); //use convertToString method to convert response to string
// Create a JSON object that we can use from the String
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
// Create a list to store are videos in
List<String> videos = new ArrayList<String>();
// Loop round our JSON list of videos creating Video objects to use within our app
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// The title of the video
String title = jsonObject.getString("title");
// The url link back to YouTube, this checks if it has a mobile url
// if it doesnt it gets the standard url
String url;
try {
url = jsonObject.getJSONObject("player").getString("mobile");
} catch (JSONException ignore) {
url = jsonObject.getJSONObject("player").getString("default");
}
// A url to the thumbnail image of the video
// We will use this later to get an image using a Custom ImageView
// Found here http://blog.blundell-apps.com/imageview-with-loading-spinner/
String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
// **add these URL's in a arrayList.
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
} catch (JSONException e) {
}
catch (Exception e) {
e.printStackTrace();
}
public static String convertToString(InputStream inputStream)
throws IOException {
if (inputStream != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"), 1024);
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
inputStream.close();
}
return writer.toString();
} else {
return "";
}
}
0 comments:
Post a Comment