How to pass in android a RPC Json with nested parameters
سؤال
What is the right code to pass a json with nested parameters in this form
{"method":"startSession",
"params": [ "email": "testmail@test.it",
"password": "1234",
"stayLogged": "1",
"idClient": "ANDROID"
]
}
to a webservice URL that receive RPC??
the webservice code is
@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);
}
المحلول
I'm going to assume you are using json-rpc 1.0 as there is no version indicator in your request.
First your missing your "id", so add that to the request.
Now here are 3 different things you could try.
1) You need to use an object {} instead of array [] if you are going to set name and value pairs. Like:
{"method":"startSession",
"params": { "email": "testmail@test.it",
"password": "1234",
"stayLogged": "1",
"idClient": "ANDROID"
},
"id":100
}
2) If your json deserializer is requiring the array syntax [] then you may have to wrap your object {} in [] like:
{"method":"startSession",
"params": [{ "email": "testmail@test.it",
"password": "1234",
"stayLogged": "1",
"idClient": "ANDROID"
}],
"id":101
}
3) Finally, you could also try using positional params just in an array like:
{"method":"startSession",
"params": [ "testmail@test.it",
"1234",
"1",
"ANDROID"
],
"id":102
}
Hope that helps.
لا تنتمي إلى StackOverflow