我正在编写一个应用程序,我想在其中获取 YouTube 视频列表,我可以通过使用特定用户的帐户来做到这一点,如下面的链接:
HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/uploads?v=2&alt=jsonc");
但是如果我想使用播放列表获取 YouTube 视频列表,那么我需要在我的代码中做哪些更改,比如这里我不需要用户名来获取视频。
http://gdata.youtube.com/feeds/api/playlists/PL1D5B07DD840FB46D?v=2&alt=json
原文链接: http ://www.youtube.com/playlist?list=PL1D5B07DD840FB46D
我的代码:
public class GetYouTubeUserVideosTask implements Runnable {
public static final String LIBRARY = "Library";
private final Handler replyTo;
private final String username;
public GetYouTubeUserVideosTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
}
@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/PL1D5B07DD840FB46D?v=2&alt=json");
HttpResponse response = client.execute(request);
String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
List<Video> videos = new ArrayList<Video>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
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);
}
}
}