我想将此数据发布到服务器。我得到空响应。从服务器得到什么响应
{
"test_id":5,
"user_id":null,
"org_id":2,
"schedule_id":15,
"group_id":null,
"next_section_id":"",
"current_section":
{
}
}
我创建界面
@POST("take_tests")
Call<Test_Responce> testRes (@Body JSONObject paramObject);
功能
try {
JSONObject paramObject = new JSONObject();
JSONObject current_section = new JSONObject();
paramObject.put("test_id", 5);
paramObject.put("user_id", "null");
paramObject.put("org_id", 2);
paramObject.put("schedule_id", 15);
paramObject.put("group_id", "null");
paramObject.put("current_section",current_section);
Call<Test_Responce> userCall = api_interface.testRes(paramObject);
userCall.enqueue(new Callback<Test_Responce>() {
@Override
public void onResponse(Call<Test_Responce> call, Response<Test_Responce> response) {
if (response.isSuccessful()){
Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(),"else",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Test_Responce> call, Throwable t) {
Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
尝试使用JsonObject
中的gson
(不要使用JSONObject
)
JsonObject paramObject =new JsonObject();
paramObject.addProperty("test_id", 5);
paramObject.addProperty("user_id", "null");
paramObject.addProperty("org_id", 2);
paramObject.addProperty("schedule_id", 15);
paramObject.addProperty("group_id", "null");
JsonObject current_section =new JsonObject();
paramObject.add("current_section",current_section);
并更改这样的界面
@POST("take_tests")
Call<Test_Responce> testRes(@Body JsonObject paramObject);
答案 1 :(得分:0)
这样发布数据
private JsonObject ApiJsonMap() {
JsonObject gsonObject = new JsonObject();
try {
JSONObject jsonObj_ = new JSONObject();
jsonObj_.put("key", "value");
jsonObj_.put("key", "value");
jsonObj_.put("key", "value");
JsonParser jsonParser = new JsonParser();
gsonObject = (JsonObject) jsonParser.parse(jsonObj_.toString());
//print parameter
Log.e("MY gson.JSON: ", "AS PARAMETER " + gsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
return gsonObject;
}
您需要创建gson对象
要获得更多帮助,请分步进行 检查我的回答: How to post raw whole json data using retrofit