[英]How to post JSON in the body of a retrofit request?
如何使用 Retrofit 將此原始 JSON 發送到服務器?
{
"merchant": {
"merchantUser": {
"email": "haaa@h.com",
"password": "123456"
}
}
}
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();
}
請求將是
{
"merchant": {
"merchantUser": {
"email": "amiyobiswas001@gmail.com",
"password": "123456"
}
}
}
Retrofit Post 方法將是
@POST("YourURL")
void YourMethodName(@Body json: JsonObject);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.