0

I am getting the exception

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $

when called a POST request.

According to this answer my json response do have the curly braces {}.

first my json response was-

{
    "status": {
        "status": "1",
        "message": "Entry inserted successfully"
    },
    "data": {
        "date": "24-Mar-2226",
        "month": "March",
        "party_name": "mark"
    }
}

then I combined the two separate objects in a single response object-

{
   "response": {
       "status": {
           "status": "1",
           "message": "Entry inserted successfully"
       },
       "data": {
           "date": "24-Mar-2226",
           "month": "March",
           "party_name": "mark"
       }
   }
}

but still I am getting the same error.

The GET request with retrofit is working fine but not the POST request.

This is my POST query-

  @POST("AddEntry")
    Call<ResponseClass> addMyEntry(@Body DetailsClass details);
Makarand
  • 983
  • 9
  • 27

2 Answers2

0

The exception says that a property you marked with String type but in the JSON it is an object.

With the JSON is:

{
   "response": {
       "status": {
           "status": "1",
           "message": "Entry inserted successfully"
       },
       "data": {
           "date": "24-Mar-2226",
           "month": "March",
           "party_name": "mark"
       }
   }
}

You should have some classes as below:

public class Response {
 @SerializedName("status")
 public Status status;

 @SerializedName("data")
 public Data data;
}

public class Status {
 @SerializedName("status")
 public String status;

 @SerializedName("message")
 public String message;
}

public class Data {
 @SerializedName("date")
 public String date;

 @SerializedName("month")
 public String month;

 @SerializedName("party_name")
 public String party_name;
}
John Le
  • 1,116
  • 9
  • 12
0

Please use below class structure.

This problem is common for most of developer just understand structure and move on.

public class ResponseData {
 @SerializedName("response")
  public Response response;
 }

  public class Response {
 @SerializedName("status")
 public Status status;

 @SerializedName("data")
 public Data data;
}

public class Status {
 @SerializedName("status")
 public String status;

 @SerializedName("message")
 public String message;
}

public class Data {
 @SerializedName("date")
 public String date;

 @SerializedName("month")
 public String month;

 @SerializedName("party_name")
 public String party_name;
}
Sush
  • 3,864
  • 2
  • 17
  • 35