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

前端 未结 23 2399
面向向阳花
面向向阳花 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:44

    Based on the top answer, I have a solution to not have to make POJOs for every request.

    Example, I want to post this JSON.

    {
        "data" : {
            "mobile" : "qwer",
            "password" : "qwer"
        },
        "commom" : {}
    }
    

    then, I create a common class like this:

    import java.util.Map;
    import java.util.HashMap;
    
    public class WRequest {
    
        Map data;
        Map common;
    
        public WRequest() {
            data = new HashMap<>();
            common = new HashMap<>();
        }
    }
    

    Finally, when I need a json

    WRequest request = new WRequest();
    request.data.put("type", type);
    request.data.put("page", page);
    

    The request marked annotation @Body then can pass to Retrofit.

提交回复
热议问题