编译以下源代码时出现错误:
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");
}
}
发布于 2017-09-14 19:23:38
编译错误应该指出,我猜测XmlRpcClient
没有具有String
的构造函数,如下所示:
XmlRpcClient中的
XmlRpcClient()无法应用于(java.lang.String)
实际上,XmlRpcClient
类只声明了一个默认的无参数构造器,您应该使用它来创建一个新实例。
可以使用XmlRpcClientConfigImpl
创建服务器URL配置
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-14 19:43:02
再一次出错
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.
发布于 2019-11-28 03:47:42
正如我检查并执行了您的代码一样,构造函数XmlRpcClient(String str)在JAR xmlrpc-2.0.1版本中可用,后来它被删除并添加为具有配置机制的无参数构造函数。
请检查您正在使用的JAR文件。尝试使用xmlrpc-2.0.1,它还需要commons-codec-1.13 JAR才能成功运行。
xmlrps-2.0.1JAR链接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) {
}
}
}
https://stackoverflow.com/questions/46217475
复制相似问题