【发布时间】:2020-04-14 17:08:53
【问题描述】:
虽然这个 json 看起来很简单,但我无法将其正确格式化以发布到正文中。
{
"requests": [
{
"action": "reject",
"justification": "admin reason",
"requestId": "ee4a5b4f3af54d849a63e305c13f6c8d"
}
],
"justification": "admin reason"
}
这是我目前在 Android Studio 中使用 Retrofit2 的内容
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://myurl.com/")
.addConverterFactory(GsonConverterFactory.create(gson));
JsonObject jsonObject = new JsonObject();
JsonObject jsonObjectN = new JsonObject();
jsonObject.addProperty("action", action);
jsonObject.addProperty("request_id", request_id);
jsonObject.addProperty("justification", "blablabla");
jsonObjectN.add("requests", jsonObject);
jsonObjectN.addProperty("justification", justification);
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<PostRequest> user = client.postRequests(jsonObjectN);
在客户端类中我有
@Headers("Content-Type: application/json")
@POST("/requests")
Call<PostRequest> postRequests(@Body JsonObject body
);
所以请求是这样的
Request{method=POST, url=https://myurl.com/v1.0/approver/requests, tags={class retrofit2.Invocation=com.loginci.rgcislogin.Client.postRequests() [{"requests":{"action":"reject","request_id":"23423423refsdsdgdg","justification":"blablabla"},"justification":"Action done by Admin"}]}}
非常接近,但我无法完全弄清楚如何以预期的确切数组/json 格式制作它? 希望有人足够了解 Android 和 jsonObject 以提供帮助。
【问题讨论】: