How to POST raw whole JSON in the body of a Retrofit request?

前端 未结 23 2351
面向向阳花
面向向阳花 2020-11-22 00:57

This question may have been asked before but no it was not definitively answered. How exactly does one post raw whole JSON inside the body of a Retrofit request?

See

23条回答
  •  孤街浪徒
    2020-11-22 01:56

    API Call

    @Headers("Content-Type: application/json")
    @POST("/set_data")
    Call setPreferences(@Body RequestData request);
    

    Note: Use GSON library of Retrofit

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class RequestData {
    
        @SerializedName("access_token")
        @Expose
        private String accessToken;
    
        @SerializedName("data")
        @Expose
        private Data data;
        // The above 'Data' is another similar class to add inner JSON objects. JSONObject within a JSONObject.
    
        public void setAccessToken(String accessToken) {
            this.accessToken = accessToken;
        }
    
        public void setData(Data data) {
            this.data = data;
        }
    }
    

    I guess that will help, rest all integration you might already have had and we don't need anything fancy to use above code snippet. It's working perfectly for me.

提交回复
热议问题