如何获得视频列表, HTTP:/ /gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json
我已经作出了计划,对此我能够使用这种URL的抓取视频列表:请检查的这个但这次我想在Android中使用播放列表来获取视频(S)的名单像使用的这个 URL,实际的YouTube视频的网址:
下面我使用JSON但现在我不知道什么样的变化,我的codeI需要做的就是用上面的播放列表中的网址视频列表。
@覆盖
公共无效的run(){
尝试 {
HttpClient的客户端=新DefaultHttpClient();
HttpUriRequest请求=新HTTPGET(http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json);
//获取响应YouTube上发回
HTT presponse响应= client.execute(要求);
//将这个响应为可读的字符串
字符串jsonString = StreamUtils.convertToString
(response.getEntity()的getContent());
//创建一个我们可以从字符串中使用JSON对象
JSONObject的JSON =新的JSONObject(jsonString);
//获取的搜索结果项目
JSONArray jsonArray = json.getJSONObject(数据)getJSONArray(项目)。
//创建一个列表存储在视频
名单<视频>视频=新的ArrayList<视频>();
的for(int i = 0; I< jsonArray.length();我++){
的JSONObject的JSONObject = jsonArray.getJSONObject(我);
//视频的标题
字符串标题= jsonObject.getString(标题);
字符串URL;
尝试 {
URL = jsonObject.getJSONObject(玩家)的getString(默认)。
}赶上(JSONException忽略){
URL = jsonObject.getJSONObject(玩家)的getString(默认)。
}
字符串thumbUrl = jsonObject.getJSONObject(缩略图)的getString(sqDefault)。
videos.add(新的视频(标题,URL,thumbUrl));
}
库LIB =新库(用户名,视频);
捆绑数据=新包();
data.putSerializable(图书馆,LIB);
消息味精= Message.obtain();
msg.setData(数据);
replyTo.sendMessage(MSG);
}赶上(ClientProtocolException E){
Log.e(Feck,E);
}赶上(IOException异常E){
Log.e(Feck,E);
}赶上(JSONException E){
Log.e(Feck,E);
}
}
解决方案
字符串JKYouTubeActivity.YOUTUBE_INFO_URL=http://gdata.youtube.com/feeds/api/playlists/_ID_?v=2&alt=json
私人字符串的getURL(字符串ID)抛出IOException异常,JSONException {
HttpClient的客户端=新DefaultHttpClient();
HTTPGET clientGetMethod =新HTTPGET(JKYouTubeActivity.YOUTUBE_INFO_URL.replace(_ ID_,ID));
HTT presponse clientResponse = NULL;
clientResponse = client.execute(clientGetMethod);
字符串infoString = _convertStreamToString(clientResponse.getEntity()的getContent());
字符串urldata =新JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
返回新JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
}
私人字符串_convertStreamToString(InputStream的IS){
的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
StringBuilder的S B =新的StringBuilder();
串线= NULL;
尝试 {
而((行= reader.readLine())!= NULL)
{
sB.append(线).append(\ N);
}
}赶上(IOException异常E){
e.printStackTrace();
} 最后 {
尝试 {
iS.close();
}赶上(IOException异常E){
e.printStackTrace();
}
}
返回sB.toString();
}
}
获取urldata后,您可以流,或做任何你想做的。同样也可以得到缩略图和标题太
How to get 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 list of videos by using this kind of URL : please check this but this time I want to get 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 the changes in my code i need to do to get the list of videos using 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 are 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);
}
}
解决方案
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