0

I want to get the youtube video title from a url so I found this code below (IOUtils) is depreciated any other way to do this

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

second way i tried

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


        }
  }

This code gets a an error Cleartext HTTP traffic to www.youtube.com not permitted so I found this answer Android 8: Cleartext HTTP traffic not permitted but I am still getting some error I don't understand.

Laurel
  • 5,965
  • 14
  • 31
  • 57

1 Answers1

0

I solved this problem by using the volley library.

My requested url was:

String Video_id = "jhjgN2d7yok";
String url = "https://www.youtube.com/oembed?url=youtube.com/watch?v=" +Video_id+ "&format=json";