1

In an api I have to send a JSonArray within a JSonObject. The JSonObject is this:

{
    "token" : "1",
    "task_name" : "Name",
    "task_desc" : "Description",
    "task_start_date" : "2020/10/01",
    "task_end_date" : "2020/10/02",
    "task_award" : "15",
    "child_ids" : [
          {
              "child_id" : "1"
          },
          {
              "child_id" : "2"
          }
      ]
}

When I send this request with Postman, it works fine and the response is receiving in a way I want. But when implementing it in android app, I get an error which says:

retrofit2.adapter.rxjava2.httpexception http 500 internal server error

So it is obvious it's not a server-side bug because it works fine in Postman.

Here is the interface that I use for my api:

interface CreateTaskRemote {

    @FormUrlEncoded
    @POST("parent_create_task")
    fun createTask(
        @Field("token") parentToken: String,
        @Field("task_name") title: String,
        @Field("task_desc") description: String,
        @Field("task_start_date") startDate: String,
        @Field("task_end_date") endDate: String,
        @Field("task_award") award: String,
        @Field("child_ids") childIds: List<ChildId>     <-- here i'm sending array
    ): Single<BooleanResponse>
}

And the ChildId class:

data class ChildId(
    @SerializedName("child_id") val id: Int
)

It seems there is a bug with implementing JSON Array. But I searched a lot on the internet and I couldn't find a solution which works for my situation.

aminography
  • 21,986
  • 13
  • 70
  • 74

1 Answers1

2

Maybe the cause is that you are posting child_id as an integer value, but you should post it as a string. (Regarding to: "child_id" : "1")

data class ChildId(
    @SerializedName("child_id") val id: String
)

Edit: Try with this configuration too:

data class CreateTaskArgs (
    @SerializedName("token") val token : Int,
    @SerializedName("task_name") val taskName : String,
    @SerializedName("task_desc") val taskDesc : String,
    @SerializedName("task_start_date") val taskStartDate : String,
    @SerializedName("task_end_date") val taskEndDate : String,
    @SerializedName("task_award") val taskAward : Int,
    @SerializedName("child_ids") val childIds : List<ChildId>
)
@POST("parent_create_task")
fun createTask(
    @Body args: CreateTaskArgs
): Single<BooleanResponse>
aminography
  • 21,986
  • 13
  • 70
  • 74