首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XmlRpcClient无法分配URL地址

XmlRpcClient无法分配URL地址
EN

Stack Overflow用户
提问于 2017-09-14 19:07:31
回答 3查看 498关注 0票数 0

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

代码语言:javascript
运行
复制
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");
  }

}
EN

回答 3

Stack Overflow用户

发布于 2017-09-14 19:23:38

编译错误应该指出,我猜测XmlRpcClient没有具有String的构造函数,如下所示:

XmlRpcClient中的

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

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

可以使用XmlRpcClientConfigImpl创建服务器URL配置

代码语言:javascript
运行
复制
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");
  }

}
票数 1
EN

Stack Overflow用户

发布于 2017-09-14 19:43:02

再一次出错

代码语言:javascript
运行
复制
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.
票数 0
EN

Stack Overflow用户

发布于 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版本完成的工作示例

客户端代码

代码语言:javascript
运行
复制
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);
        }
    }
}

服务器代码

代码语言:javascript
运行
复制
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) {
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46217475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档