2014-10-20 8 views
-1

, как я могу получить идентификатор видео YouTube, который является частью списка воспроизведения например, ссылку, как это:Как получить идентификатор видео YouTube из PlaylistID?

https://www.youtube.com/watch?v=kXYiU_JCYtU&list=PL87FA01F68C540290

это GData

http://gdata.youtube.com/feeds/api/playlists/PL87FA01F68C540290?v=2&alt=jsonc&max-results=50

Это мой код

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(); 
     } 
    } 
} 

}

ответ

0

Как я вижу здесь, у вас есть JSON, сформированная строка в качестве ответа.

Существует много API-интерфейсов JSON, которые вы можете использовать в 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"); 
} 

Вам нужно обернуть все это в примерочных поймать блок.

EDIT

Как я понял, вам нужно разобрать строки у вас есть в списке links.

for(String link : links) { 
    String id = link.split("&")[0].split("v=")[1]; 
    // do what you need with the link 
} 
+0

Я бы на самом деле просто способ извлечь эту строку kXYiU_JCYtU указывает это https://www.youtube.com/watch?v=kXYiU_JCYtU&list=PL87FA01F68C540290 – john

+0

Затем просто разобрать строку 'url.split («&») [0] ' –

+0

извините, но что бы я сделал с этим? :) – john

Смежные вопросы