-1

enter image description here

@Headers("Content-Type: application/json")
@POST("answer")
fun sendAnswer(@Header("token") token :String, @Body answer: SendAnswerModel):Call<SendAnswerResultModel>

here is my model class

data class Vote2019Answer(
    @SerializedName("question_id") @Expose val id: Int,
    @SerializedName("answer") @Expose val answer: String)

another model class

data class SendAnswerModel(
    @SerializedName("answer") @Expose val answer: List<Vote2019Answer> )
freeman
  • 92
  • 11

2 Answers2

1

You can pass json object / json array using @Body by converting the json model to POJO ( using GSON) .

Check this out !

Community
  • 1
  • 1
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
1

Try the following way.

Your API endpoint

@POST("url")
Call<ResponseBody> yourAPi(@Body JSONArray jsonArray);

And API call using param data

String[] answer = {"aaa","bbb","ccc"}
JSONArray jsArray= new JSONArray();
try {
    for(int i = 0; i < answer.length; i++) {
        JSONObject object = new JSONObject();
        object.put("question_id",i+1);
        object.put("answer",answer[i]);
        jsArray.put(object);
    }

} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}   
// here you can call your api 
Call<ResponseBody> call =  yourApiService.yourAPi(jsArray);
// ........

Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29