Issue
How to get a list of videos from http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json
I have made a program in which I am able to fetch a list of videos by using this kind of URL: please check this but this time I want to get a list of video(s) using Playlist in Android like using this URL, actual YouTube videos URL:
Here I am using JSON but now I don't know what are changes in my code I need to do to get the list of videos using the above playlist URL.
@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json");
// Get the response that YouTube sends back
HttpResponse response = client.execute(request);
// Convert this response into a readable string
String jsonString = StreamUtils.convertToString
(response.getEntity().getContent());
// Create a JSON object that we can use from the String
JSONObject json = new JSONObject(jsonString);
// Get is search result items
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
// Create a list to store are videos in
List<Video> videos = new ArrayList<Video>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// The title of the video
String title = jsonObject.getString("title");
String url;
try {
url = jsonObject.getJSONObject("player").getString("default");
} catch (JSONException ignore) {
url = jsonObject.getJSONObject("player").getString("default");
}
String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
videos.add(new Video(title, url, thumbUrl));
}
Library lib = new Library(username, videos);
Bundle data = new Bundle();
data.putSerializable(LIBRARY, lib);
Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg);
} catch (ClientProtocolException e) {
Log.e("Feck", e);
} catch (IOException e) {
Log.e("Feck", e);
} catch (JSONException e) {
Log.e("Feck", e);
}
}
Solution
String JKYouTubeActivity.YOUTUBE_INFO_URL=http://gdata.youtube.com/feeds/api/playlists/_ID_?v=2&alt=json
private String getUrl(String id) throws IOException, JSONException {
HttpClient client = new DefaultHttpClient();
HttpGet clientGetMethod = new HttpGet(JKYouTubeActivity.YOUTUBE_INFO_URL.replace("_ID_", id));
HttpResponse clientResponse = null;
clientResponse = client.execute(clientGetMethod);
String infoString = _convertStreamToString(clientResponse.getEntity().getContent());
String urldata=new JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
return new JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
}
private String _convertStreamToString(InputStream iS) {
BufferedReader reader = new BufferedReader(new InputStreamReader(iS));
StringBuilder sB = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null)
{
sB.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
iS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sB.toString();
}
}
After getting the urldata you can streamed it or do whatever you want to. Same as well you can get Thumbnails and title too
Answered By - Vikalp Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.