How to retrieve details of single video from youtube using videoID through Data API v3.0 in Android?

前端 未结 3 1611
误落风尘
误落风尘 2021-01-03 02:54

My server sends the list of videoID to Android. Now, I want to show Title, Thumbnail and Number of Comments on these videos in List View. I have done this in web using GET r

相关标签:
3条回答
  • 2021-01-03 03:23

    Refer my post here. I just tried this method for my project and it works very nicely. You don't need the above code or any google api jar imports. Just replace the HTTP request with your HTTP request.

    Output is returned in JSON, for which you can use a JSON parser jar to retrieve the title,thumbnails and other details you may require, as I have described in my answer there.

    0 讨论(0)
  • 2021-01-03 03:30

    Try this:

    protected void requestYoutubeVideos(String text) {
          try {
              youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
                  public void initialize(HttpRequest request) throws IOException {
                  }
              }).setApplicationName("My app name").build();
    
              // Define the API request for retrieving search results.
              YouTube.Search.List query = youtube.search().list("id");
    
              // Set your developer key from the Google Cloud Console for
              // non-authenticated requests. See:
              // https://cloud.google.com/console
              query.setKey(YOUTUBE_API_KEY);
              query.setQ(text);
              query.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
    
              // To increase efficiency, only retrieve the fields that the
              // application uses.
              query.setFields("items(id)");
              query.setOrder("viewCount");
    
              // Restrict the search results to only include videos. See:
              // https://developers.google.com/youtube/v3/docs/search/list#type
              query.setType("video");
    
              SearchListResponse searchResponse = query.execute();
              List<SearchResult> list = searchResponse.getItems();
              Log.e("Youtube search", "list ===> " + list);
    
              //Get Info for each video id
              for (SearchResult video: list) {
                  youtubeList.add(video);
    
                  YouTube.Videos.List query2 = youtube.videos().list("id,contentDetails,snippet,statistics").setId(video.getId().getVideoId());
                  query2.setKey(YOUTUBE_API_KEY);
                  query2.setMaxResults((long) 1);
                  query2.setFields("items(id,contentDetails,snippet,statistics)");
    
                  VideoListResponse searchResponse2 = query2.execute();
                  List<Video> listEachVideo = searchResponse2.getItems();
                  Video eachVideo = listEachVideo.get(0);
    
              }
    
          } catch (GoogleJsonResponseException e) {
              Log.e("Youtube search", "There was a service error: " + e.getDetails().getCode() + " : "
                      + e.getDetails().getMessage());
          } catch (IOException e) {
              Log.e("Youtube search", "There was an IO error: " + e.getCause() + " : " + e.getMessage());
          } catch (Throwable t) {
              t.printStackTrace();
          }
      }
    

    and do not forget to call it from another thread:

     new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                requestYoutubeVideos("Harry el Sucio Potter");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }).start();
    
    0 讨论(0)
  • 2021-01-03 03:42

    YouTube provides (at least) two official libraries relevant to your question:

    • YouTube Android Player API
    • YouTube Data API Client Library for Java

    As the name already suggests, the first library is specifically developed for the Android platform. Its focus is on enabling you to incorporate video playback functionality into an app by providing a player framework. If your goal is to enable users to simply play YouTube videos, then is probably easiest to implement. Do note that this library requires the official YouTube app to be installed on the device.

    The second library is more generic (although there are separate instructions for using it on Android) and provides a wrapper around YouTube's Data API to make interfacing with it a little easier. Hence, it allows you to do basically everything you can also do with the web API. As such, it solves a different problem than the Android Player API and is more likely the way to go if you want full control over how you display video data in your own UI.

    Your third option would be to do exactly what you did for your web-based solution: make the API call yourself, parse the response and bind up the relevant data to your UI components. Various networking libraries (i.e. Retrofit) can greatly simplify this process.

    0 讨论(0)
提交回复
热议问题