如何使用嵌套参数传入android的RPC Json

时间:2012-03-01 23:30:38

标签: android json json-rpc

使用此表单中的嵌套参数传递json的正确代码是什么

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

到接收RPC的Web服务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);
}

1 个答案:

答案 0 :(得分:2)

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

首先,您错过了“id”,因此请将其添加到请求中。

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

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

{"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
}

希望有所帮助。