http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=jsonから動画のリストを取得する方法
このようなURLを使って動画のリストを取得できるプログラムを作成しました。これを確認してください。ただし、今回は、このURLを使用するように、Androidのプレイリストを使用して動画のリストを取得したいと思います。 YouTubeビデオのURL:
ここではJSONを使用していますが、上記の再生リスト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);
}
}