android - 如何在android中传递一个带有嵌套参数的RPC Json

标签 android json json-rpc

以这种形式传递带有嵌套参数的 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/

相关文章:

android - Android 应用程序中的安全警报

java - android 可以与 JRE 7 一起使用吗?

android - 如何将 WebView 设置为非全屏?

go - 什么是rpc网络地址?

c++ - 用于嵌入式平台的 C++ 中的简单 JSON-RPC?

php - PHP 的 JSON-RPC 2 客户端能够调用嵌套类中的方法

android - 删除列表中的行

javascript:如何获取 json 对象的最高层作为数组?

android - 对象数组的 jackson 反序列化

javascript - 使用 JavaScript 应用程序将 JSON 文件保存到服务器