0

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?

Anantha Babu
  • 216
  • 2
  • 14

1 Answers1

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