简体   繁体   中英

How to post JSON in the body of a retrofit request?

How can I send this raw JSON into sever using Retrofit?

{
    "merchant": {
        "merchantUser": {
            "email":    "haaa@h.com",
            "password": "123456"

        }
    }
}

You can directly use the HashMap to send json in @Body parameter for POST request

interface Code {
  @POST("/json")
  CodeResponse postJson(@Body HashMap<String, Object> body);
}
        JsonObject reqObjectParent = new JsonObject();
        JsonObject reqObject = new JsonObject();
        try {

            JsonObject reqObjectChild = new JsonObject();
            reqObjectChild.addProperty("email", "amiyobiswas001@gmail.com");
            reqObjectChild.addProperty("password", "123456");

            reqObject.add("merchantUser",reqObjectChild);
            reqObjectParent.add("merchant",reqObject);

            System.out.println("Request"+reqObjectParent);

        } catch (Exception e) {
            e.printStackTrace();
        }

Request will be

{
    "merchant": {
        "merchantUser": {
            "email":    "amiyobiswas001@gmail.com",
            "password": "123456"

        }
    }
}

Retrofit Post method will be

 @POST("YourURL")
 void YourMethodName(@Body json: JsonObject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM