简体   繁体   English

如何在改造请求的正文中发布 JSON?

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

How can I send this raw JSON into sever using Retrofit?如何使用 Retrofit 将此原始 JSON 发送到服务器?

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

        }
    }
}

You can directly use the HashMap to send json in @Body parameter for POST request POST请求可以直接使用HashMap在@Body参数中发送json

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 Retrofit Post 方法将是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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