I need to execute the publish methode with an unknown number of parameters. Each attachment needs to be added using ...
Parameter.with("attached_media[i]", ...
but the number of attachments can change. How do I dynamically add parameters in this call?
GraphResponse publishMessageResponseMSg =
facebookClient.publish("me/feed", GraphResponse.class,
Parameter.with("message", msg),
Parameter.with("published", "false"),
Parameter.with("scheduled_publish_time", timeStampStr),
Parameter.with("attached_media[0]", "{\"media_fbid\":\""+ publishMessageResponseImg1.getId() +"\"}"),
Parameter.with("attached_media[1]", "{\"media_fbid\":\""+ publishMessageResponseImg2.getId() +"\"}"));
This is rather simple, you can put an array directly into the publish
method.
// create ArrayList with Parameter objects
List<Parameter> paramList = new ArrayList<>();
paramList.add(Parameter.with("message",msg));
paramList.add(Parameter.with("published","false"));
paramList.add(Parameter.with("scheduled_publish_time",timeStampStr));
for (int i=0; i < 5; i++) {
paramList.add(Parameter.with("attached_media["+ i +"]",FileFactory.getFileJson(i));
}
// convert List to array
Parameter[] params = list.stream().toArray(Parameter[]::new);
// publish everything
GraphResponse publishMessageResponseMSg =
facebookClient.publish("me/feed", GraphResponse.class, params);