如何 POST JSONObject / JSONArray in POST Retrofit?
How to POST JSONObject / JSONArray in POST Retrofit?
我想将这些数据作为 JSONObject 发送到服务器
{
"userId": "5f06ca75ce77e8e2cdd2f320",
"services": [
{
"serviceId": "5eeb4d5f2d4f7288d8959f68"
},
{
"serviceId": "5eeb5b942b567b6a8fee6b00"
}
]
}
这是我制作上述格式的按钮点击,它正在完美创建
binding.btnFeedback.setOnClickListener(v -> {
JSONObject jsonOBJ = new JSONObject();
try {
JSONArray list = new JSONArray();
jsonOBJ.put("userId", "5f06ca75ce77e8e2cdd2f320");
String[] dataArray = {"5eeb4d5f2d4f7288d8959f68","5eeb5b942b567b6a8fee6b00"};
for( int i=0;i<dataArray.length;i++) {
JSONObject internalObject = new JSONObject();
internalObject.put("serviceId",dataArray[i]);
list.put(internalObject);
}
jsonOBJ.put("services", list);
} catch (JSONException e) {
Toast.makeText(getActivity(), ""+e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
mViewModel.apiCallArray(jsonOBJ);
});
}
这是我的改造块
APIService services = RetroClass.getApiClient(My.token).create(APIService.class);
Call<TestResponse> allUsers = services.test(list);
allUsers.enqueue(new Callback<TestResponse>() {
@Override
public void onResponse(@NotNull Call<TestResponse> call, @NotNull Response<TestResponse> response) {
progressBar.setValue(8);
if(response.isSuccessful()){
showToast(getApplication(),""+response);
}
else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
showToast(getApplication().getApplicationContext(),jObjError.getString("msg"));
} catch (Exception e) {
Log.d("", e.getMessage());
showToast(getApplication(),""+e.getMessage());
}
}
}
@Override
public void onFailure(Call<TestResponse> call, Throwable t) {
progressBar.setValue(8);
showToast(getApplication().getApplicationContext(),t.getMessage());
}
});
这是我的界面class
@POST("profile/update-interests")
Call<TestResponse> test(@Body JSONObject jsonArray);
logcat 中的错误信息如下:
I/okhttp.OkHttpClient: <-- 422 无法处理的实体
内容类型:application/json
但是当我点击 post 按钮时,它 return 响应正文为空并显示错误“消息无值”。谁能帮我解决这个问题?
我遇到了同样的问题,我在将 JSONObject 更改为 JsonObject 并将 JSONArray 更改为 JsonArray 后修复了它。Here is documentation。所以只需更新您的那个部分,我希望它会起作用。
binding.btnFeedback.setOnClickListener(v -> {
JsonObject jsonOBJ = new JsonObject();
try {
JsonArray list = new JsonArray();
jsonOBJ.put("userId", "5f06ca75ce77e8e2cdd2f320");
String[] dataArray = {"5eeb4d5f2d4f7288d8959f68","5eeb5b942b567b6a8fee6b00"};
for( int i=0;i<dataArray.length;i++) {
JSONObject internalObject = new JSONObject();
internalObject.put("serviceId",dataArray[i]);
list.put(internalObject);
}
jsonOBJ.put("services", list);
} catch (JSONException e) {
Toast.makeText(getActivity(), ""+e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
mViewModel.apiCallArray(jsonOBJ);
});
}
我想将这些数据作为 JSONObject 发送到服务器
{
"userId": "5f06ca75ce77e8e2cdd2f320",
"services": [
{
"serviceId": "5eeb4d5f2d4f7288d8959f68"
},
{
"serviceId": "5eeb5b942b567b6a8fee6b00"
}
]
}
这是我制作上述格式的按钮点击,它正在完美创建
binding.btnFeedback.setOnClickListener(v -> {
JSONObject jsonOBJ = new JSONObject();
try {
JSONArray list = new JSONArray();
jsonOBJ.put("userId", "5f06ca75ce77e8e2cdd2f320");
String[] dataArray = {"5eeb4d5f2d4f7288d8959f68","5eeb5b942b567b6a8fee6b00"};
for( int i=0;i<dataArray.length;i++) {
JSONObject internalObject = new JSONObject();
internalObject.put("serviceId",dataArray[i]);
list.put(internalObject);
}
jsonOBJ.put("services", list);
} catch (JSONException e) {
Toast.makeText(getActivity(), ""+e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
mViewModel.apiCallArray(jsonOBJ);
});
}
这是我的改造块
APIService services = RetroClass.getApiClient(My.token).create(APIService.class);
Call<TestResponse> allUsers = services.test(list);
allUsers.enqueue(new Callback<TestResponse>() {
@Override
public void onResponse(@NotNull Call<TestResponse> call, @NotNull Response<TestResponse> response) {
progressBar.setValue(8);
if(response.isSuccessful()){
showToast(getApplication(),""+response);
}
else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
showToast(getApplication().getApplicationContext(),jObjError.getString("msg"));
} catch (Exception e) {
Log.d("", e.getMessage());
showToast(getApplication(),""+e.getMessage());
}
}
}
@Override
public void onFailure(Call<TestResponse> call, Throwable t) {
progressBar.setValue(8);
showToast(getApplication().getApplicationContext(),t.getMessage());
}
});
这是我的界面class
@POST("profile/update-interests")
Call<TestResponse> test(@Body JSONObject jsonArray);
logcat 中的错误信息如下: I/okhttp.OkHttpClient: <-- 422 无法处理的实体 内容类型:application/json
但是当我点击 post 按钮时,它 return 响应正文为空并显示错误“消息无值”。谁能帮我解决这个问题?
我遇到了同样的问题,我在将 JSONObject 更改为 JsonObject 并将 JSONArray 更改为 JsonArray 后修复了它。Here is documentation。所以只需更新您的那个部分,我希望它会起作用。
binding.btnFeedback.setOnClickListener(v -> {
JsonObject jsonOBJ = new JsonObject();
try {
JsonArray list = new JsonArray();
jsonOBJ.put("userId", "5f06ca75ce77e8e2cdd2f320");
String[] dataArray = {"5eeb4d5f2d4f7288d8959f68","5eeb5b942b567b6a8fee6b00"};
for( int i=0;i<dataArray.length;i++) {
JSONObject internalObject = new JSONObject();
internalObject.put("serviceId",dataArray[i]);
list.put(internalObject);
}
jsonOBJ.put("services", list);
} catch (JSONException e) {
Toast.makeText(getActivity(), ""+e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
mViewModel.apiCallArray(jsonOBJ);
});
}