Hannes Wallnöæer ¤Ï Java ÍÑ XML-RPC ¤ÎÍ¥¤ì¤¿¼ÂÁõ¤òÄ󶡤·¤Æ¤¤¤Þ¤¹¡£
¥¤¥ó¥¹¥È¡¼¥ë¤Ï¡¢ÇÛÉÛʪ¤ò¥À¥¦¥ó¥í¡¼¥É¤·¤Æ¡¢unzip ¤·¤Æ¡¢ CLASSPATH ¤Ë *.jar ¥Õ¥¡¥¤¥ë¤òÄÉ ²Ã¤·¤Þ¤¹¡£Unix ¥·¥¹¥Æ¥à¾å¤Ç¡¢¼¡¤ÎÆþÎϤˤè¤ê¤³¤Îºî¶È¤¬¹Ô¤¨¤Þ¤¹¡£
bash$ unzip xmlrpc-java.zip bash$ cd xmlrpc-java/lib bash$ CLASSPATH=`pwd`/openxml-1.2.jar:`pwd`/xmlrpc.jar:$CLASSPATH |
¼¡¤Î¥×¥í¥°¥é¥à¥³¡¼¥É¤ò JavaClient.java ¤È¤·¤Æ¥Õ¥¡¥¤¥ë¤ËÊݸ¤·¤Æ¤¯¤À ¤µ¤¤¡£
import java.util.Vector; import java.util.Hashtable; import helma.xmlrpc.*; public class JavaClient { // The location of our server. private final static String server_url = "http://xmlrpc-c.sourceforge.net/api/sample.php"; public static void main (String [] args) { try { // Create an object to represent our server. XmlRpcClient server = new XmlRpcClient(server_url); // Build our parameter list. Vector params = new Vector(); params.addElement(new Integer(5)); params.addElement(new Integer(3)); // Call the server, and get our result. Hashtable result = (Hashtable) server.execute("sample.sumAndDifference", params); int sum = ((Integer) result.get("sum")).intValue(); int difference = ((Integer) result.get("difference")).intValue(); // Print out our result. System.out.println("Sum: " + Integer.toString(sum) + ", Difference: " + Integer.toString(difference)); } catch (XmlRpcException exception) { System.err.println("JavaClient: XML-RPC Fault #" + Integer.toString(exception.code) + ": " + exception.toString()); } catch (Exception exception) { System.err.println("JavaClient: " + exception.toString()); } } } |
¼¡¤Î¥×¥í¥°¥é¥à¥³¡¼¥É¤ò JavaServer.java ¤È¤·¤Æ¥Õ¥¡¥¤¥ë¤ËÊݸ¤·¤Æ¤¯¤À ¤µ¤¤¡£
import java.util.Hashtable; import helma.xmlrpc.*; public class JavaServer { public JavaServer () { // Our handler is a regular Java object. It can have a // constructor and member variables in the ordinary fashion. // Public methods will be exposed to XML-RPC clients. } public Hashtable sumAndDifference (int x, int y) { Hashtable result = new Hashtable(); result.put("sum", new Integer(x + y)); result.put("difference", new Integer(x - y)); return result; } public static void main (String [] args) { try { // Invoke me as <http://localhost:8080/RPC2>. WebServer server = new WebServer(8080); server.addHandler("sample", new JavaServer()); } catch (Exception exception) { System.err.println("JavaServer: " + exception.toString()); } } } |