java – How to call youtube API with multiple parameters?

Question:

I'm trying to get the data from several videos, but the Youtube API is separate for videos and playlist , so I used the playlist API to get the VideoID of each video that is in a certain playlist , but to call the videos API I want call multiple videos at once to avoid making too many 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 reading about string maps, but it doesn't seem to be what I need.

Currently I'm using RecyclerView own Adapter passing the position inside the onBindViewHolder but this, in addition to several unnecessary requests, brings me problems like the items in the list loading during the scroll , it's very bad.

Answer:

At first you can create a list of videos, then concatenate them together creating a StringBuilder . Look

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 on 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 you have any more questions regarding the Youtube API parameters, you can check the documentation .

Scroll to Top
AllEscort