【问题标题】:How to pass in android a RPC Json with nested parameters如何在android中传递一个带有嵌套参数的RPC Json
【发布时间】:2012-03-01 23:30:37
【问题描述】:

以这种形式传递带有嵌套参数的json的正确代码是什么

 {"method":"startSession",
"params": [ "email": "testmail@test.it", 
            "password": "1234", 
            "stayLogged": "1", 
            "idClient": "ANDROID"
           ]
}

到接收 RPC 的网络服务 URL??

webservice 代码是

 @Webservice(paramNames = {"email", "password", "stayLogged", "idClient"},
public Response startSession(String email, String password, Boolean stayLogged, String idClient) throws Exception {
    boolean rC = stayLogged != null && stayLogged.booleanValue();
    UserService us = new UserService();
    User u = us.getUsersernamePassword(email, password);
    if (u == null || u.getActive() != null && !u.getActive().booleanValue()) {
        return ErrorResponse.getAccessDenied(id, logger);
    }
    InfoSession is = null;
    String newKey = null;
    while (newKey == null) {
        newKey = UserService.md5(Math.random() + " " + new Date().getTime());
        if (SessionManager.get(newKey) != null) {
            newKey = null;
        } else {
            is = new InfoSession(u, rC, newKey);
            if (idClient != null && idClient.toUpperCase().equals("ANDROID")) {
                is.setClient("ANDROID");
            }
            SessionManager.add(newKey, is);
        }
    }
    logger.log(Level.INFO, "New session started: " + newKey + " - User: " + u.getEmail());
    return new Response(new InfoSessionJson(newKey, is), null, id);
}

【问题讨论】:

    标签: android json json-rpc


    【解决方案1】:

    我假设您使用的是 json-rpc 1.0,因为您的请求中没有版本指示符。

    首先你缺少你的“id”,所以将它添加到请求中。

    现在您可以尝试 3 种不同的方法。

    1) 如果要设置名称和值对,则需要使用对象 {} 而不是数组 []。 喜欢:

    {"method":"startSession",
    "params": { "email": "testmail@test.it", 
                "password": "1234", 
                "stayLogged": "1", 
                "idClient": "ANDROID"
               },
     "id":100
    }
    

    2) 如果您的 json 反序列化器需要数组语法 [],那么您可能必须将对象 {} 包装在 [] 中,例如:

    {"method":"startSession",
    "params": [{ "email": "testmail@test.it", 
                "password": "1234", 
                "stayLogged": "1", 
                "idClient": "ANDROID"
               }],
      "id":101
    }
    

    3) 最后,您还可以尝试在数组中使用位置参数,例如:

    {"method":"startSession",
    "params": [ "testmail@test.it", 
                "1234", 
                "1", 
                "ANDROID"
               ],
       "id":102
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-03-24
      • 2014-02-27
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 2020-11-17
      • 1970-01-01
      • 2015-11-12
      相关资源
      最近更新 更多