json post body help, using retrofit 和 jsonObject
json post body help, using retrofit and jsonObject
就这么简单 json 看起来我无法在正文中将其正确格式化为 post。
{
"requests": [
{
"action": "reject",
"justification": "admin reason",
"requestId": "ee4a5b4f3af54d849a63e305c13f6c8d"
}
],
"justification": "admin reason"
}
这是我目前在 Android 工作室中使用 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);
在客户端class我有
@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"}]}}
非常接近,但我不太明白如何按照预期的方式制作它的 array/json 格式??
希望有人足够了解 Android 和 jsonObject 来提供帮助。
您没有添加 json 数组请求
试试下面的方法。
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();
JsonArray jsonArray = new JsonArray();
jsonObject.addProperty("action", action);
jsonObject.addProperty("request_id", request_id);
jsonObject.addProperty("justification", "blablabla");
jsonArray.add(jsonObject);
jsonObjectN.add("requests", jsonArray);
jsonObjectN.addProperty("justification", justification);
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<PostRequest> user = client.postRequests(jsonObjectN);
就这么简单 json 看起来我无法在正文中将其正确格式化为 post。
{
"requests": [
{
"action": "reject",
"justification": "admin reason",
"requestId": "ee4a5b4f3af54d849a63e305c13f6c8d"
}
],
"justification": "admin reason"
}
这是我目前在 Android 工作室中使用 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);
在客户端class我有
@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"}]}}
非常接近,但我不太明白如何按照预期的方式制作它的 array/json 格式?? 希望有人足够了解 Android 和 jsonObject 来提供帮助。
您没有添加 json 数组请求
试试下面的方法。
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();
JsonArray jsonArray = new JsonArray();
jsonObject.addProperty("action", action);
jsonObject.addProperty("request_id", request_id);
jsonObject.addProperty("justification", "blablabla");
jsonArray.add(jsonObject);
jsonObjectN.add("requests", jsonArray);
jsonObjectN.addProperty("justification", justification);
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<PostRequest> user = client.postRequests(jsonObjectN);