简体   繁体   中英

Android Retrofit - send dynamic number of POST parameters

I need to send a dynamic number of POST parameter values to an endpoint (there could be 1 or there could be 50). All of them will have the same key value.

Is this possible? I can't seem to figure out how to create a RequestBody that encompasses something like this, even when I try to construct it in plain text.

I have the list of strings prepared for it, but I just don't know how to create this kind of thing. The endpoint works in PostMan when I input a lot of post form parameters with the same key value, so the endpoint is setup properly for it. I'm just not sure if Retrofit supports this kind of thing, and I cannot seem to find any info around it.

I'm currently working with Java instead of Kotlin. Thoughts?

You can also pass multiple field parameter to your request like this:

@FormUrlEncoded
@POST("/oauth/access_token")
Call<YourResponseObject> sendData(
    @FieldMap Map<String, String> params
);

The map can take variable number of args.

So you can pass data like:

/*
map = { "field1_key"="value1", "field2_key"="value2", "field3_key"="value3", ...}
*/
retrofit().create(YourInterface.class).sendData(mapOfFields)

ps: retrofit() is a method that returns a Retrofit instance to work with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM