To get the YouTube feed for the User
change the URL as per the username, here i have shared the dummy URL,
sample JSON Response click here
SocialMediaFeedItem.java
I have taken values from JSON are Video file Thumb image, Title and Description.
change the URL as per the username, here i have shared the dummy URL,
sample JSON Response click here
private static class YoutubeReader {
private final String URL = "https://gdata.youtube.com/feeds/api/users/FLGatorsAthletics/uploads?v=2&alt=json";
private FeedListener feedListener;
public void getFeed(FeedListener feedListener){
this.feedListener = feedListener;
new GetYouTubeFeed().execute();
}
private class GetYouTubeFeed extends AsyncTask<Void,Void,String>{
@Override
protected String doInBackground(Void... params) {
return getYoutubeFeed();
}
@Override
protected void onPostExecute(String response) {
if(response != null){
ArrayList<SocialMediaFeedItem> twitter_feeds = parseYouTubeFeed(response);
if(twitter_feeds != null)
feedListener.onSuccess(twitter_feeds);
else
feedListener.onError("Error in parsing Youtube JSON response");
}else{
feedListener.onError("Error in getting Youtube Feed ");
}
}
}
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 + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private String getYoutubeFeed() {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
InputStream inStream = httpEntity.getContent();
String result = convertStreamToString(inStream);
inStream.close();
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private final String YOUTUBE_TAG_FEED = "feed";
private final String YOUTUBE_TAG_ENTRY = "entry";
private final String YOUTUBE_TAG_TITLE = "title";
private final String YOUTUBE_TAG_LINK = "link";
private final String YOUTUBE_TAG_HREF = "href";
private final String YOUTUBE_TAG_MEDIA_GROUP = "media$group";
private final String YOUTUBE_TAG_DESCRIPTION = "media$description";
private final String YOUTUBE_TAG_THUMBNAIL = "media$thumbnail";
private final String YOUTUBE_TAG_URL = "url";
private final String YOUTUBE_TAG_TEXT = "$t";
private ArrayList<SocialMediaFeedItem> parseYouTubeFeed(String response) {
if (response == null)
return null;
ArrayList<SocialMediaFeedItem> feeds = new ArrayList<SocialMediaFeedItem>();
try {
JSONObject jObj = new JSONObject(response);
JSONObject feedObj = jObj.getJSONObject(YOUTUBE_TAG_FEED);
JSONArray entryArr = feedObj.getJSONArray(YOUTUBE_TAG_ENTRY);
for (int i = 0; i < entryArr.length(); i++) {
JSONObject entryData = entryArr.getJSONObject(i);
String heading = entryData.getJSONObject(YOUTUBE_TAG_TITLE)
.getString(YOUTUBE_TAG_TEXT);
URL video_link = new URL(entryData.getJSONArray(YOUTUBE_TAG_LINK).getJSONObject(0).getString(YOUTUBE_TAG_HREF));
String summary =entryData
.getJSONObject(YOUTUBE_TAG_MEDIA_GROUP)
.getJSONObject(YOUTUBE_TAG_DESCRIPTION)
.getString(YOUTUBE_TAG_TEXT);
URL url = new URL(entryData
.getJSONObject(YOUTUBE_TAG_MEDIA_GROUP)
.getJSONArray(YOUTUBE_TAG_THUMBNAIL).getJSONObject(0)
.getString(YOUTUBE_TAG_URL));
SocialMediaFeedItem feedItem = new SocialMediaFeedItem(heading,summary,url,null,video_link);
feeds.add(feedItem);
}
} catch (JSONException e) {
e.printStackTrace();
}catch (MalformedURLException e) {
e.printStackTrace();
}
return feeds;
}
}
SocialMediaFeedItem.java
public class SocialMediaFeedItem {
public SocialMediaFeedItem(String heading, String summary, URL imageURL, Date datePosted,
URL link) {
this.heading =heading;
this.summary =summary;
this.imageURL =imageURL;
this.datePosted = datePosted;
this.link =link;
}
public String getHeading() {
return heading;
}
public String getSummary() {
return summary;
}
public URL getImageURL() {
return imageURL;
}
public Date getDatePosted() {
return datePosted;
}
public URL getLink() { return link; }
private String heading;
private String summary;
private URL imageURL;
private Date datePosted;
private URL link;
}
I have taken values from JSON are Video file Thumb image, Title and Description.
No comments:
Post a Comment