android - 我想发布嵌套的 json 数据

标签 android retrofit2

我想将此数据发布到服务器。我收到空响应。什么从服务器得到响应

 {
    "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();
                }

最佳答案

尝试使用 gson 中的 JsonObject(不要使用 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);

关于android - 我想发布嵌套的 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114569/

相关文章:

android - map 在设备上不起作用

android - 协程 worker : interrupt doWork() on cancelPendingIntent action

android - Retrofit 通过 wifi 成功返回数据,但通过移动数据失败

android - 改造 2.2.0 Android API 24 javax.net.ssl.SSLHandshakeException : Handshake failed

android - 向 OkHttpClient 添加多个拦截器

android - RxJava,改造错误:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

android - Google Play 商店发布检查

android - 无法在 RoomDB Dao 中使用挂起功能

android - 如何从 SDCard 加载图像到 Image Switcher?

android - 改造 body 返回空