Retrofit request with multiple parameters in kotlin not working correct

My code in the Api interface:

 interface APIServiceUpdateTeam {
    @FormUrlEncoded
    @PUT("/api/v1/updateTeam/{Id}/")
    suspend fun updateTeam(@Path("Id")teamId: String,
                           @Header("Authorization") token: String,
                           @FieldMap params: 
            HashMap<String?,ArrayList<*>>): Response<ResponseBody>,
    
   }

//my code the request call

val params = HashMap<String?, ArrayList<*>>()
params.put("members",teamMemberListToUpdate)
params.put("name",teamIdToAdd)
params.put("password", passwordTeamAdd)


val response =
    service.updateTeam(teamIdToAdd.toString(),"Token ${token}", params) 

//So the post/put request is 2 times a string (name and password) and 1 Array of integers, int his example members. In my example here the HashMap is an array of integers so the name and password are not accepted.

2 answers

  • answered 2020-11-25 09:07 Petr Kubáč

    If I understand correctly you want to specify value type as Any type

    val params = HashMap<String?, Any>()
    params.put("members",teamMemberListToUpdate)
    params.put("name",teamIdToAdd)
    params.put("password", passwordTeamAdd)
    

    This way you can put different types into map

  • answered 2020-11-25 15:39 Dali Hamza

    you shouldn't change the password like that you need to make it separatly from the PUT. you can make your put like this :

     @Headers(
            "Content-Type: application/json"
        )
        @PUT("/api/v1/updateTeam/{id}")
        suspend fun modify(
            @Header("Authorization") authorization: String,
            @Path("id") id: String,
            @Body team: Team,
        ): Response<ResponseBody>
    

    or

    @FormUrlEncoded
    @PUT("/api/v1/updateTeam/{id}")
      suspend fun modify(
          @Header("Authorization") authorization: String,
          @Path("id") id: String,
          @Field("membres") member: List<Any>,
          @Field("name") name: String,
      ): Response<ResponseBody>