如何按用户获取YouTube视频

时间:2012-01-23 05:55:45

标签: android video youtube

我想在列表视图中显示某个YouTube频道的视频,并允许用户选择要观看的视频之一。是否有任何Json数据可用于此。?

修改

JSONObject json = JSONfunctions.getJSONfromURL("http://gdata.youtube.com/feeds/api/videos? 
                  author=NationalGeographic&v=2&alt=json");    

try{
    JSONArray  earthquakes = json.getJSONArray("feed");    
     for(int i=0;i<earthquakes.length();i++){                       
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = earthquakes.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("name", "Earthquake name:" + e.getString("eqid"));
            map.put("magnitude", "Magnitude: " +  e.getString("magnitude"));
            mylist.add(map);            
        }       
    }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

我有权限和工作在标签中的网址,我得到nullpointer错误@JSONArray earthquakes = json.getJSONArray(“feed”);

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

您可以点击以下网址,将USERNAME替换为相关用户:

http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?v=2&alt=jsonc

您可以测试来电here

修改

如果您正在寻找国家地理,请点击的网址为:

http://gdata.youtube.com/feeds/api/users/NationalGeographic/uploads?v=2&alt=jsonc

你会发现这样的视频网址:

JSONObject json = JSONfunctions.getJSONfromURL("http://gdata.youtube.com/feeds/api/users/NationalGeographic/uploads?v=2&alt=jsonc");    
final JSONObject data = json.getJSONObject("data"); 
final JSONArray array = data.getJSONArray("items");
for (int i = 0; i < array.length(); i++) {
    final JSONObject item = array.getJSONObject(i);
    final JSONObject player = item.getJSONObject("player");
    final String url = player.getString("default"));
    // The url is that of the video to be played
}

为简洁起见,省略了try / catch代码。