0

I am using RetroFit for the first time, so it is confusing me. I am trying to pass a JSON object to the server via POST. However I don't know how can make the model classes and the methods for the retrofit library. My JSON looks like this:

{
  "Header": {
    "UserDetails": "sample string 1",
    "ClientCode": "sample string 2",
    "Password": "sample string 3",
    "ViewType": 0,

  },
  "MemberDetails": {
    "Username": "sample string 1",
    "Password": "sample string 2",
    "MemberNo": "sample string 3",
    "MobileNo": "sample string 4"
  }
}

I have generate 2 model classes: Header and MemberDetails

The Interface class is like this:

public interface MyServerAPI {
    @POST("users/memberlogin")
    Call<> login(@Body WHAT_OBJECT_TO_PASS_HERE?);
}

I don't know whether to pass both Header and MemberDetails object or put them in a ArrayList and pass that to the login().

user2498079
  • 2,872
  • 8
  • 32
  • 60
  • Here is your answer http://stackoverflow.com/a/21423093/2394266 – Huteri Feb 25 '16 at 08:34
  • Possible duplicate of [How to POST raw whole JSON in the body of a Retrofit request?](http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request) – Seelenvirtuose Feb 25 '16 at 08:37
  • Just create another class that has Header and MemberDetails class inside. – onur taskin Feb 25 '16 at 08:41

1 Answers1

0

The Interface class can be like this

    public interface MyServerAPI {
      @FormUrlEncoded
      @POST("users/memberlogin")
        Call<MyResponse> login(@Field("my_object") String myObject);
    }

Then you can pass your object as a json string like this

String jsonString=new Gson().toJson(obj);
Call<MyResponse> myResponse = myGithubApi.login(jsonString);
Darish
  • 11,032
  • 5
  • 50
  • 70