Reputation: 752
What is the easiest way to get the title of a single YouTube video in Android? I'm using the Android YouTube API to play video's but they don't return a title cause it only supports playback options. The only thing I need is to display the title of the video, I obviously have the video ID itself but I don't have a clue where to start.
Things I looked at:
For some reason I find it hard to understand how to use them and this seems like a really simple action to me. I'm curious if there is an easier option or if there is someone who can help me get on the right track with this.
Upvotes: 12
Views: 11435
Reputation: 1679
Thank you @Castiblanco To those who are lazy to find the jars and imports
http://commons.apache.org/proper/commons-io/
http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
Upvotes: 1
Reputation: 1198
You can use this code for this purpose.
public class SimpleYouTubeHelper {
public static String getTitleQuietly(String youtubeUrl) {
try {
if (youtubeUrl != null) {
URL embededURL = new URL("http://www.youtube.com/oembed?url=" +
youtubeUrl + "&format=json"
);
return new JSONObject(IOUtils.toString(embededURL)).getString("title");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Upvotes: 31