0

编译以下源代码时出现错误:

import java.util.*;
import org.apache.xmlrpc.client.*; 
import org.apache.xmlrpc.common.*;
import org.apache.xmlrpc.*;
public class pms {
    public static void main (String [] args) {
        String UserName = "123";
        String Password = "123";
        String pKey     = "123";
        XmlRpcClient server = new XmlRpcClient("http://localhost/RPC2"); //("http://localhost/RPC2"); 
        Vector params = new Vector();
      try {
         params.addElement(new Integer(17));
         params.addElement(new Integer(13));
         Object result = server.execute("acquire_token",params);
         int sum = ((Integer) result).intValue();
         System.out.println("The sum is: "+ sum);
      } catch (Exception exception) {
         System.err.println("JavaClient: " + exception);
      }
        System.out.println("Hello World");
  }

}
4

3 回答 3

1

编译错误应该说明我猜没有构造函数来XmlRpcClient拥有String,如下所示:

XmlRpcClient 中的 XmlRpcClient() 无法应用于 (java.lang.String)

实际上,XmlRpcClient该类仅声明了一个默认的无参数构造函数,您应该使用它来创建一个新实例。

可以使用以下命令创建服务器URLXmlRpcClientConfigImpl配置:

import java.util.*;
import org.apache.xmlrpc.client.*; 
import org.apache.xmlrpc.common.*;
import org.apache.xmlrpc.*;

public class pms {
    public static void main (String [] args) {
        String UserName = "123";
        String Password = "123";
        String pKey     = "123";

        // create a configuration instance with the requested URL
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost/RPC2"));

        // create the client and configure it with instantiated configuration
        XmlRpcClient server = new XmlRpcClient();
        server.setConfig(config);

        Vector params = new Vector();
      try {
         params.addElement(new Integer(17));
         params.addElement(new Integer(13));
         Object result = server.execute("acquire_token",params);
         int sum = ((Integer) result).intValue();
         System.out.println("The sum is: "+ sum);
      } catch (Exception exception) {
         System.err.println("JavaClient: " + exception);
      }
        System.out.println("Hello World");
  }

}
于 2017-09-14T11:23:37.837 回答
0

正如我已经检查并执行了您的代码,构造函数 XmlRpcClient(String str) 在 JAR xmlrpc-2.0.1版本中可用,后来它被删除并添加为具有配置机制的无参数构造函数。

请检查您使用的是哪个 JAR 文件。尝试使用 xmlrpc-2.0.1,它还需要commons-codec-1.13 JAR 才能成功运行。

xmlrps-2.0.1 JAR 链接http://archive.apache.org/dist/ws/xmlrpc/binaries

您可以查看使用 JDK 1.8 上提到的 JAR 版本完成的工作示例

客户代码

import java.util.Vector;
import org.apache.xmlrpc.XmlRpcClient;

public class JavaRpcClient {

    public static void main(String[] args) {
        try {
            XmlRpcClient client = new XmlRpcClient("http://localhost:8080/RPC2");
            Vector params = new Vector();

            params.addElement(new Integer(17));
            params.addElement(new Integer(10));

            Object result = client.execute("sample.sum", params);

            int sum = ((Integer) result).intValue();
            System.out.println("The sum is: " + sum);

        } catch (Exception exception) {
            System.err.println("JavaClient: " + exception);
        }
    }
}

服务器代码

import org.apache.xmlrpc.WebServer;

public class JavaRpcServer {

    public Integer sum(int num1, int num2) {
        return new Integer(num1 + num2);
    }

    public static void main(String[] args) {
        try {
            System.out.println("Attempting to start XML-RPC Server...");

            WebServer server = new WebServer(8080);
            server.addHandler("sample", new JavaRpcServer());
            server.start();

            System.out.println("Started successfully.");
            System.out.println("Accepting requests. (Halt program to stop.)");

        } catch (Exception e) {
        }
    }
}
于 2019-11-27T19:47:42.227 回答
0

再次出错

pms.java:22: error: cannot find symbol
                server.setconfig(config);
                      ^
  symbol:   method setconfig(XmlRpcClientConfigImpl)
  location: variable server of type XmlRpcClient
Note: pms.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
于 2017-09-14T11:43:01.697 回答