How to call youtube API with multiple parameters?

Asked

Viewed 199 times

3

I’m trying to get the data from various videos, but the Youtube API is separate for videos and playlist, then I used the API of playlist to obtain the VideoID of each video in a given playlist, but to call the video API I want to call multiple videos at once to avoid making several unnecessary API calls like this:

https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=VIDEOID1,VIDEOID2,VIDEOID3...&key={API_KEY}

How to pass these videoid separated by comma using Retrofit? I’ve been researching and read about map of strings, but it doesn’t seem to be what it needs.

I’m currently using myself Adapter of RecyclerView passing the position within the onBindViewHolder but this in addition to several unnecessary requests, brings me problems like list items carrying the scroll, it gets really bad.

1 answer

1


At first you can create a list of videos, then concatenate creating a StringBuilder. Behold

List <String> list = new ArrayList <String> ();
list.add("video_codigo_1");
list.add("video_codigo_2");
list.add("video_codigo_3");
list.add("video_codigo_4");

StringBuilder strB = new StringBuilder();
for (int i = 0; i < list.size(); i++) {

    if (i < (list.size() - 1))
        strB.append(list.get(i)).append(",");
    else strB.append(list.get(i));
}
String url = "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id="
   + strB.toString() + "&key={API_KEY}";

See working in IDEONE.

An adaptation for Retrofit,

class SuaActivity extends Activity {

    private static final String BASEPATH = "https://www.googleapis.com/youtube/v3/";

    private interface API {
        @GET("/videos")
        void getVideos(@QueryMap Map <String, String> 
            params, new Callback <String> callback);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        RestAdapter rest = new RestAdapter.Builder().setEndpoint(BASEPATH).build();
        API service = rest.create(API.class);

        List < String > list = new ArrayList < String > ();
        list.add("video_codigo_1");
        list.add("video_codigo_2");
        list.add("video_codigo_3");
        list.add("video_codigo_4");

        // concatena as strings separando por virgula
        StringBuilder strB = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {

            if (i < (list.size() - 1))
            strB.append(list.get(i)).append(",");
            else strB.append(list.get(i));
        }

        Map <String, String> params = new HashMap <String, String> ();
        params.put("part", "snippet,statistics");
        params.put("id", strB.toString());
        // aqui podes colocar outros parâmetros

        service.getVideos(params, new Callback <String> () {
            // aqui você faz o tratamento para listar os videos
        });
    }
}

If there are any further questions regarding the Youtube API parameters, you can check in the documentation.

  • 1

    worked well, had not known to use the string Builder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.