我需要发布多个json对象

时间:2018-07-25 12:14:54

标签: android retrofit

我需要发布多个json对象。我想将此数据发布到服务器。我得到空值。在这里,我附加了api和函数。

我的json对象:

{
    "test_id":5,
    "user_id":null,
    "org_id":2,
    "schedule_id":15,
    "group_id":null,
    "next_section_id":"",
    "current_section":
       {

        }
    }

在这里,我附加了api和函数。 功能:

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

2 个答案:

答案 0 :(得分:0)

尝试从gson使用JsonObject(不要使用JSONObject)

DECLARE @json NVARCHAR(MAX)
    ,@namestart int
    ,@Q1 int
    ,@Q2 int
    ,@After varchar(100)
    ,@before varchar(100)

SET @json='{"code":"0123456" ,"name":"example" ,"table":"exampletable" ,"addedby":"exampleperson" ,"dateadded":1520333304750, "qualifier":[{"name":"Qualifier" ,"value":"examplevalue" ,"code":"123456", "prefix":"[?] "}] ,"prefix":"[?] " ,"suffix":""} ,{"code":"68566005" ,"name":"example2" ,"table":"exampletable2", "addedby":"exampleperson2" ,"dateadded":1519874550441, "qualifier":[{"name":"Qualifier" ,"value":"examplevalue2" ,"code":"415684004 ","prefix":"[?] "}] ,"prefix":"[?] " ,"suffix":""';

set @namestart = charindex('"name"',@json)

set @Q1 = CHARINDEX('"',@json, @namestart + len('"name"'))

set @Q2 =  CHARINDEX('"',@json ,  @Q1+1)

set @After = substring(@json, @q1+1,@q2-@q1-1)

--select @namestart , @Q1 ,@Q2, @After

declare @priorPiece varchar(100)

set @priorPiece = left(@json,@namestart-1)

set @Q1 = charindex('"',reverse(@priorPiece ))
set @q2 = charindex('"',reverse(@priorPiece ),@q1+1)
set @before = reverse(substring(reverse(@priorPiece ),@q1+1,@q2-@q1-1))

select @before,@after

并更改这样的界面

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);

答案 1 :(得分:0)

您应该使用JSONObject.NULL而不是"null",例如

paramObject.put("user_id", JSONObject.NULL);