xml-rpc, java, and eclipse

This writeup serves as a quick summary of my initial investigation of writing xml-rpc applications using java in Eclipse. It's hoped to be useful to whoever is reading this.
 
xml-rpc is a popular web service protocol. In case you haven't heard this before, one common application is publishing blogs from your desktop.
 
Popular open source XML-RPC library implementation in Java includes:
Apache XML-RPC
Redstone XML-RPC Library

Redstone library is very small, only 39Kb JAR (server and client) or a 20Kb JAR (client only). This is the library I am going to use here.

Eclipse is a popular IDE for Java development. A good tutorial website for Eclipse/Java beginners is available here eclipsetutorial

The Java code example here is based on http://phpcode.mypapit.net/redstone-simple-and-compact-java-xml-rpc-library/9/

To start, you need to create a new java project in Eclipse, and create package, java files with names you like. Then, download Redstone xml-rpc library, unzip it, and add the jar files to your project's CLASSPATH. What I do is create a "lib" folder in your project folder and copy the "xmlrpc-client-1.1.jar" file in to the lib folder. To add this library to your project's path, right click your project and select "properties->Java Build Path->libraries->add external JARs", finally select redstone library.

To use Redstone library in a XML-RPC client application, you can either use the redstone.xmlrpc.XmlRpcClient class or the redstone.xmlrpc.XmlRpcProxy. Since the latter is recommended by the authors, I am going to use XmlRpcProxy in this example.

foxrate.org's xml-rpc api is as follows:

RPC endpoint: http://foxrate.org/rpc/

Method name: foxrate.currencyConvert

Parameters:

  • from currency (eg: USD) = string
  • to currency (eg: GBP) = string
  • amount to convert (eg:100.0) = float

First, we create a java file (foxrate.java) containing the interface

package org.test;

public interface foxrate {
Object currencyConvert(String from, String to, double amount);

}

Then we create another java file (test.java) which contains the main function:

package org.test;
import java.net.URL;
import redstone.xmlrpc.*;
public class test {
   public static void main( String[] args ) throws   Exception
  {
      foxrate client = ( foxrate ) XmlRpcProxy.createProxy( new URL("http://foxrate.org/rpc/"), new Class[] { foxrate.class }, false);

      Object token = client.currencyConvert("USD", "CNY", 1.0);

      XmlRpcStruct struct = (XmlRpcStruct)token;
      System.out.println(new        Double(struct.getDouble("amount")).toString());
  }
}

Ok. That's it for this extremely simple tutorial. Let me know if this works for you. If you are a guru on java/xml-rpc/eclipse, I welcome your comments as I am pretty new to these stuff.


Back to top