Java 如何从PlayID获取YouTube视频ID?
如何获取作为播放列表一部分的youtube视频的ID 下面是一个链接示例: 这是gdata 这是我的密码Java 如何从PlayID获取YouTube视频ID?,java,android,youtube,youtube-api,playlist,Java,Android,Youtube,Youtube Api,Playlist,如何获取作为播放列表一部分的youtube视频的ID 下面是一个链接示例: 这是gdata 这是我的密码 public class Episodi extends Activity implements OnClickListener { ImageView mainThumb; TextView mainTitle; TextView mainTime; LinearLayout videos; ArrayList<String> links; URL jsonURL;
public class Episodi extends Activity implements OnClickListener {
ImageView mainThumb;
TextView mainTitle;
TextView mainTime;
LinearLayout videos;
ArrayList<String> links;
URL jsonURL;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.episodi);
new ParseVideoDataTask().execute();
mainThumb = (ImageView) findViewById(R.id.mainThumb);
mainTitle = (TextView) findViewById(R.id.mainTitle);
mainTime = (TextView) findViewById(R.id.mainTime);
videos = (LinearLayout) findViewById(R.id.videos);
}
public class ParseVideoDataTask extends AsyncTask<String, String, String> {
int count = 0;
@Override
protected String doInBackground(String... params) {
URL jsonURL = null;
URLConnection jc;
links = new ArrayList<String>();
try {
jsonURL = new URL("http://gdata.youtube.com/feeds/api/playlists/" +
"PL87FA01F68C540290" +
"?v=2&alt=jsonc&max-results=50");
jc = jsonURL.openConnection();
InputStream is = jc.getInputStream();
String jsonTxt = IOUtils.toString(is);
JSONObject jj = new JSONObject(jsonTxt);
JSONObject jdata = jj.getJSONObject("data");
JSONArray aitems = jdata.getJSONArray("items");
for (int i=0;i<aitems.length();i++) {
JSONObject item = aitems.getJSONObject(i);
JSONObject video = item.getJSONObject("video");
String title = video.getString("title");
JSONObject player = video.getJSONObject("player");
String link = player.getString("default");
String length = video.getString("duration");
JSONObject thumbnail = video.getJSONObject("thumbnail");
String thumbnailUrl = thumbnail.getString("hqDefault");
String[] deets = new String[4];
deets[0] = title;
deets[1] = thumbnailUrl;
deets[2] = length;
links.add(link);
publishProgress(deets);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(final String... deets) {
count++;
if (count == 1) {
Picasso.with(getBaseContext()).load(deets[1]).into(mainThumb);
mainTitle.setText(deets[0]);
mainTime.setText(formatLength(deets[2]));
mainThumb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Episodi.this,Video.class);
//I want to insert .putExtra here for the VIDEO ID (example. kXYiU_JCYtU)
startActivity(i);
}
});
} else {
LayoutInflater layoutInflater = (LayoutInflater)Episodi.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View video = layoutInflater.inflate(R.layout.videothumb, null);
ImageView thumb = (ImageView) video.findViewById(R.id.thumb);
TextView title = (TextView) video.findViewById(R.id.title);
TextView time = (TextView) video.findViewById(R.id.time);
Picasso.with(getBaseContext()).load(deets[1]).into(thumb);
title.setText(deets[0]);
time.setText(formatLength(deets[2]));
video.setPadding(20, 20, 20, 20);
videos.addView(video);
video.setId(count-1);
video.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Episodi.this,Video.class);
//I want to insert .putExtra here for the VIDEO ID (example. kXYiU_JCYtU)
startActivity(i);
}
});
}
}
}
private CharSequence formatLength(String secs) {
int secsIn = Integer.parseInt(secs);
int hours = secsIn / 3600,
remainder = secsIn % 3600,
minutes = remainder / 60,
seconds = remainder % 60;
return ((minutes < 10 ? "0" : "") + minutes
+ ":" + (seconds< 10 ? "0" : "") + seconds );
}
public void onDestroy() {
super.onDestroy();
for (int i=0;i<videos.getChildCount();i++) {
View v = videos.getChildAt(i);
if (v instanceof ImageView) {
ImageView iv = (ImageView) v;
((BitmapDrawable)iv.getDrawable()).getBitmap().recycle();
}
}
}
公共类情节扩展活动实现OnClickListener{
ImageView主拇指;
文本视图主标题;
文本视图主时间;
线性布局视频;
ArrayList链接;
URL jsonURL;
/**在首次创建活动时调用*/
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
setContentView(右布局,情节);
新建ParseVideoDataTask().execute();
mainThumb=(ImageView)findViewById(R.id.mainThumb);
maintTitle=(TextView)findViewById(R.id.maintTitle);
mainTime=(TextView)findViewById(R.id.mainTime);
视频=(线性布局)findViewById(R.id.videos);
}
公共类ParseVideoDataTask扩展了AsyncTask{
整数计数=0;
@凌驾
受保护的字符串doInBackground(字符串…参数){
URL jsonURL=null;
URLConnection jc;
links=newarraylist();
试一试{
jsonURL=新URL(“http://gdata.youtube.com/feeds/api/playlists/" +
“PL87FA01F68C540290”+
“?v=2&alt=jsonc&max results=50”);
jc=jsonURL.openConnection();
InputStream=jc.getInputStream();
字符串jsonTxt=IOUtils.toString(is);
JSONObject jj=新的JSONObject(jsonTxt);
JSONObject jdata=jj.getJSONObject(“数据”);
JSONArray aitems=jdata.getJSONArray(“项目”);
对于(inti=0;i,正如我在这里看到的,您将JSON格式的字符串作为响应
有很多JSON API,您可以使用Android中提供的。只需制作JSONObject
并获取您感兴趣的数据即可
JSONObject playlist = new JSONObject(response);
// list items
JSONArray items = playlist.getJSONObject("data").getJSONArray("items");
// traverse items array and get video url
for(int i=0; i<items.length(); i++) {
JSONObject item = items.getJSONObject(i);
// video url
String videoURL = item.getJSONObject("video").getJSONObject("player").getString("default");
}
我真的想用一种方法来提取这一行kXYiU_JCYtU指出这一点,然后只解析字符串url.split(&“[0]”
对不起,我该怎么办?:)对不起,我没有看到你的全部评论。应该是这样的url.split(&”)[0]。split(“v=”[1]
url
是您的youtube链接。它可以这样工作吗?String ad=links.split(&”)[0]。split(“v=”)[1];给我以下错误(类型ArrayList的方法split(String)未定义)
for(String link : links) {
String id = link.split("&")[0].split("v=")[1];
// do what you need with the link
}