I need to pass json object as body in retrofit. but creating pojo classes seems like mess. is it possible to send json object without creating pojo class?
Asked
Active
Viewed 1,111 times
0
-
1You can use `HashMap` – Rohit5k2 Feb 22 '16 at 14:00
-
BTW using POJO is much better. – Rohit5k2 Feb 22 '16 at 14:01
-
@Rohit5k2 . but i have json object inside have another json object inside have json array means. if i create POJO for this , certainly getting confuse. thats why i asked question. – Anantha Babu Feb 22 '16 at 14:11
-
You should see this http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request – Rohit5k2 Feb 22 '16 at 14:14
-
1@Rohit5k2 thanks . i will try that. – Anantha Babu Feb 23 '16 at 06:12
1 Answers
0
Well it's not replying directly the question but (for me) pojos are the way to go when you can generate them so easily and keep the project clean by using @AutoValue library.
Import in your gradle app file
provided 'com.google.auto.value:auto-value:1.2' apt 'com.google.auto.value:auto-value:1.2' apt 'com.github.rharter:auto-value-gson:0.2.5'
Create object with autovalue:
@AutoValue public abstract class MyObject { @SerializedName("my_data") public abstract String myData(); @SerializedName("some_extra_data") public abstract ExtraData extraData(); public static TypeAdapter<MyObject> typeAdapter(Gson gson) { return new AutoValue_MyObject.GsonTypeAdapter(gson); } } @AutoValue public abstract class ExtraData { @SerializedName("my_extra_data") public abstract String myExtraData(); @SerializedName("my_extra_data2") public abstract String myExtraData2(); public static TypeAdapter<ExtraData> typeAdapter(Gson gson) { return new AutoValue_ExtraData.GsonTypeAdapter(gson); } }
Send it to with your Service
@POST("somedata/data/") Call<Data> sendData (MyObject request);

Vincent D.
- 977
- 7
- 20