无法使用xml rpc通过java客户端创建Drupal用户

时间:2011-05-05 14:54:37

标签: java drupal xml-rpc

我有这个代码来创建一个受this启发的用户 执行后我有这个错误:

  

尝试创建新用户时发生错误
  用户名字段是必需的   需要电子邮件地址字段   密码字段是必需的。
  线程“main”中的异常org.apache.xmlrpc.XmlRpcException:需要用户名字段。
  电子邮件地址字段是必需的。

这是我的代码:

public boolean createUser(DrupalAccount account) throws Exception
{
    Vector<Object> params = generateDefaultParams(MethodUserCreate);
    // code can be re-factored to use ArrayList<Object>
    //ArrayList<Object> params = generateDefaultParams(MethodUserCreate);
    // must also re-factor generateDefaultParams method
    // currently objects don't work for submitting parameters to the
    // user.save service. Use an array instead

    params.add(account);
    try
    {
        Object o = xmlRpcClient.execute(MethodUserCreate, params);
        if (log.isLoggable(Level.FINEST))
        {
            log.finest(MethodUserCreate + " returned " + o.toString());
            System.out.println(o.toString());
        }
    }
    catch(Exception e)
    {
        System.err.println("An error occurred when attempting to create a user");
        System.err.println(e.getMessage());
        throw e;
    }

    //TODO add code to inform if user successfully created
    return true;
}

修改:我尝试过MathRo的建议但仍然会出错:

Exception in thread "main" org.apache.xmlrpc.XmlRpcException:
Server error. Requested method user.create not specified.
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:197)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:156)
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143)
    at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69)
    at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:137)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:126)
    at DrupalService.userCreate(DrupalService.java:284)
    at Main.main(Main.java:29)

虽然我在我的服务类中添加了这个:

public HashMap userCreate(String name, String mail, String pass)
        throws XmlRpcException {
    HashMap paramUser = new HashMap();
    paramUser.put("name", name);
    paramUser.put("mail",mail);
    paramUser.put("pass",pass);
    Object[] param = {paramUser};
    HashMap ret_createUser = (HashMap)xmlRpcClient.execute("user.create", param);
    return  ret_createUser;
}

在我的主要课程中:

service.userCreate("myname", "me@mydomain.com", "mypass");

1 个答案:

答案 0 :(得分:0)

好的,我不习惯Drupal 6和服务-6但我会尽力帮助(我正在使用D7)。

您是否尝试通过自己创建参数来调用 user.save 方法(不使用DruaplAccount对象)?

例如,对于我的JavaClient(在D7上使用服务3.x),我创建了一个创建用户的函数:

/**
 * Create a user 
 * @param name : 
 * @param mail
 * @param pass
 * @return
 * @throws XmlRpcException
 */
public HashMap userCreate(String name, String mail, String pass)
        throws XmlRpcException {
    HashMap paramUser = new HashMap();
    paramUser.put("name", name);
    paramUser.put("mail",mail);
    paramUser.put("pass",pass);
    Object[] param = {paramUser};
    HashMap ret_createUser =
            (HashMap) this.client.execute("user.create", param );
    return  ret_createUser;
}

我不知道它是否非常有用,但我发布以防万一。