XmlRpcClient cant assign URL address

732 views Asked by At

I have an error while compiling below source code:

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");
  }

}
2

There are 2 answers

0
tmarwen On

The compilation error should state as I guess that there is no constructor for XmlRpcClient having String, something as below:

XmlRpcClient() in XmlRpcClient cannot be applied to (java.lang.String)

In fact the XmlRpcClient class declares only a default no-arg constructor which you should use to create a new instance.

The server URL configuration can be created using XmlRpcClientConfigImpl:

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");
  }

}
0
TulaskarRahul On

as i have checked and executed your code, the constructor XmlRpcClient(String str) is available in JAR xmlrpc-2.0.1 version and later on it is removed and added as no-args constructor with configuration mechanism.

Please check which JAR file you are using. Try with xmlrpc-2.0.1, also it requires commons-codec-1.13 JAR to run successfully.

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

You can check out working example done with mentioned JAR versions on JDK 1.8

Client Code

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);
        }
    }
}

Server Code

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) {
        }
    }
}