以这种形式传递带有嵌套参数的 json 的正确代码是什么
{"method":"startSession",
"params": [ "email": "testmail@test.it",
"password": "1234",
"stayLogged": "1",
"idClient": "ANDROID"
]
}
到接收 RPC 的网络服务 URL??
网络服务代码是
@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);
}
最佳答案
我假设您使用的是 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
}
希望对您有所帮助。
关于android - 如何在android中传递一个带有嵌套参数的RPC Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9525843/