2017-07-13 121 views
0

假设我有这个链接“https://www.youtube.com/watch?v=VgC4b9K-gYU”。现在我想获得这个视频的标题,并希望显示在我的WebView()... 如何做到这一点?如何从视频url获得YouTube视频标题

这是我的代码

public class MainActivity extends AppCompatActivity { 

Button btn; 
WebView browser; 
EditText url_text; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    openURL(); 
} 

public void openURL(){ 
    btn = (Button) findViewById(R.id.button); 
    url_text =(EditText) findViewById(R.id.editText); 
    browser = (WebView) findViewById(R.id.webView); 

    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String url = "https://www.youtube.com/watch?v=VgC4b9K-gYU"; 

      browser.getSettings().setLoadsImagesAutomatically(true); 
      browser.getSettings().setJavaScriptEnabled(true); 
      browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
      browser.loadUrl(url); 
      browser.getSettings().getDisplayZoomControls(); 
     } 
    }); 
} 

}

我只是想获得和打印视频的标题网址中包含

回答

0

可以使用this

URL: https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY 
    &fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics 

Description: This example modifies the fields parameter from example 3 so that in the API response, each video resource's snippet object only includes the channelId, title, and categoryId properties. 

API response: 

{ 
"videos": [ 
    { 
    "id": "7lCDEYXw3mM", 
    "snippet": { 
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw", 
    "title": "Google I/O 101: Q&A On Using Google APIs", 
    "categoryId": "28" 
    }, 
    "statistics": { 
    "viewCount": "3057", 
    "likeCount": "25", 
    "dislikeCount": "0", 
    "favoriteCount": "17", 
    "commentCount": "12" 
    } 
    } 
] 
}