2

I have this json object of key value pairs that needs to be sent in a post request using retrofit

{
"Criteria":{
  "DisciplineId":0,
  "SeasonId":0,
  "Leagues":[

  ],
  "StartDate":"06 Sep 2013",
  "EndDate":"14 Dec 2013",
  "RoundId":0,
  "GroupId":0,
  "MatchesScores":3
},
"SearchInfo":{
  "PageNumber":1,
  "PageSize":20,
  "Sort":1,
  "TotalRecords":542
 }
}

I was thinking of creating a POJO that matches the gson definition of the json object then using the setters in the POJO class to set the value for each key value pair.

So I would have something like this

@FormUrlEncoded
@POST("/getMatches")
void getMatches(@Field("Criteria") Criteria criteria,@Field("SearchInfo") SearchInfo searchInfo, Callback<JSONKeys> keys);

Am I on the right track?

How can I achieve this seeing that there are two nested json objects within the json object as well as a json array with one of these objects?

Castell James
  • 329
  • 6
  • 23

1 Answers1

2

You could create a request class that contains both of those. As long as the names of the member variables match the json (or you use SerializedName) the conversion happens automatically.

class MyRequest{
    @SerializedName("Criteria") Criteria criteria;
    @SerializedName("SearchInfo") SearchInfo searchInfo;
}

Where Criteria is:

class Criteria {
    @SerializedName("DisciplineId")  int disciplineId;
    @SerializedName("SeasonId")      int seasonId;
    @SerializedName("Leagues")       List<Integer> leagues; // Change Integer to datatype
    @SerializedName("StartDate")     String startDate;
    @SerializedName("EndDate")       String endDate;
    @SerializedName("RoundId")       int roundId;
    @SerializedName("GroupId")       int groupId;
    @SerializedName("MatchesScores") int matchesScores;
}

And SearchInfo is:

class SearchInfo{
    @SerializedName("PageNumber")   int pageNumber;
    @SerializedName("PageSize")     int pageSize;
    @SerializedName("Sort")         int sort;
    @SerializedName("TotalRecords") int totalRecords;
}

To use try (see here):

@POST("/getMatches")
public void getMatches(@Body MyRequest request, Callback<Boolean> success);

Retrofit uses Gson internally and will automatically convert your MyRequest Object to the json format you've described in your question.


Note: Usually it's convention to name the json keys as lowercase-with-underscore, and java with camelcase. Then instead of using SerializedName everywhere, you set your key naming convention when creating your gson object (see here):

Gson gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    .create()
Community
  • 1
  • 1
bcorso
  • 45,608
  • 10
  • 63
  • 75
  • lol..this is the exact class definitions I have. My now concern is after i set the values for classes (both gson objects) can i just say `getMatches(MyRequest.getCriteria, MyRequest.getSearchInfo)`? – Castell James Jul 26 '15 at 04:48
  • @CastellJames why not just use `getMatches(MyRequest request)` ? – bcorso Jul 26 '15 at 04:53
  • My member variables match the json keys so I do not need to use `@SerializedName`, however that is a good point. I learnt something there. Seeing that there are two objects here how would the one instance of `MyRequest` work? how would I use use `getMatches(MyRequest request)`? – Castell James Jul 26 '15 at 05:11
  • try `public void getMatches(@Body MyRequest request, Callback callback);` I've updated the answer. – bcorso Jul 26 '15 at 05:28
  • 1
    Also, while using SerializedName is not related to your question, per se, I mention it in my answer because otherwise your POJO variables names must start with capital letters to match your json file. This is bad form for Java and Json, which is why I state the convention, and explain how to convert properly in the "Note". – bcorso Jul 26 '15 at 05:34
  • now the question is how to fill my request, i mean how to fill Criteria and SearchInfo with desire value – AndroidDev May 12 '17 at 08:15