How to call youtube API with several parameters?

3

I'm trying to get data from multiple videos, however 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 to call multiple videos at once to avoid making multiple unnecessary API calls like this:

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

How do I pass these comma-separated% by using Retrofit? I've been researching and reading about strings map, but it does not seem to be what I need.

I'm currently using my own videoid of Adapter passing the position within RecyclerView but this in addition to several unnecessary requests, brings me problems like list items loading during onBindViewHolder , gets very bad.

    
asked by anonymous 29.06.2017 / 17:12

1 answer

1

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

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 to 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 further questions regarding the Youtube API parameters, you can check in the documentation .

    
29.06.2017 / 18:49