[英]XmlRpcClient cant assign URL address
在編譯以下源代碼時出現錯誤:
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");
}
}
由於我猜沒有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");
}
}
再次錯誤
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.
因為我已經檢查並執行了代碼,所以構造函數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) {
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.