我如何从Android Studio的URL获得youtubeVideo标题

izj3ouym  于 2023-02-02  发布在  Android
关注(0)|答案(1)|浏览(155)

我想从一个网址获得YouTube视频标题,所以我发现下面的代码(IOUtils)是贬值的任何其他方式来做到这一点

public class SimpleYouTubeHelper {

   public static String getTitleQuietly(String youtubeUrl) {
      try {
           if (youtubeUrl != null) {
              URL embededURL = new URL("http://www.youtube.com/oembed?url=" +
              youtubeUrl + "&format=json"
          );

        return new JSONObject(IOUtils.toString(embededURL)).getString("title");
    }

} catch (Exception e) {
    e.printStackTrace();
}
return null;
}
}

我试过的第二种方法

class getYoutubeJSON extends Thread {

    String data = " ";

    @Override
    public void run() {

        try {
            URL url = new URL("http://www.youtube.com/oembed?url="+" https://www.youtube.com/watch?v=a4NT5iBFuZs&ab_channel=FilipVujovic"
                    + "&format=json");

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;

            while ((line = bufferedReader.readLine()) != null){

                 data =data + line;
            }

            if(!data.isEmpty()){

                JSONObject jsonObject = new JSONObject(data);
               // JSONArray users = jsonObject.getJSONArray("author_name");

                Log.d("RT " , jsonObject.toString());
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        }
  }

此代码获得错误不允许到www.youtube.com的明文HTTP通信因此我找到了此答案Android 8: Cleartext HTTP traffic not permitted,但我仍然获得一些我不明白的错误,有人能帮助我吗

xdyibdwo

xdyibdwo1#

我通过使用volley库解决了这个问题。
我请求的网址是:字符串url =“https://www.youtube.com/oembed?url=youtube.com/watch?v=“+视频标识+“&格式=json”;

相关问题