Sending an array of objects in retrofit put request

时间:2019-01-09 22:01:44

标签: android retrofit2 put android-networking

I am trying to send an array of json objects using retrofit, while this is working with raw JSON in postman I am having trouble in Android. Right now I am just doing one value in the array, it will do more later.

Api Interface

@Headers({
        "Content-Type:application/json"
})
@PUT("/People")
Call<Task> updatePeople(
        @retrofit2.http.Header("Authorization") String authorization, @retrofit2.http.Body List<Person> body
);

request

fun updatePeople(person: Person, legId:Int){
    val peopleList = listOf(person)
    personApi.updatePeople(token, peopleList).enqueue(object : Callback<Task>{
        override fun onFailure(call: Call<List<Person>>, t: Throwable)
        {
            //Error
        }

        override fun onResponse(call: Call<List<Person>>, response: Response<List<Person>>)
        {
            if(response.code() == 200)
            {
                //It works
            }
        }
    })
}

1 个答案:

答案 0 :(得分:1)

Try to add "hasBody = true" like this:

@FormUrlEncoded
@Headers("Content-Type: application/json")
@HTTP(method = "PUT", path = "/People", hasBody = true)
Call<Task> updatePeople(@retrofit2.http.Header("Authorization") String authorization, @retrofit2.http.Body List<Person> body);