284 lines
5.2 KiB
HTML
284 lines
5.2 KiB
HTML
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Using XML-RPC with Java</TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.63
|
|
"><LINK
|
|
REL="HOME"
|
|
TITLE="XML-RPC HOWTO"
|
|
HREF="index.html"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Using XML-RPC with C and C++"
|
|
HREF="xmlrpc-howto-c.html"><LINK
|
|
REL="NEXT"
|
|
TITLE="Using XML-RPC with PHP"
|
|
HREF="xmlrpc-howto-php.html"></HEAD
|
|
><BODY
|
|
CLASS="section"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>XML-RPC HOWTO</TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="xmlrpc-howto-c.html"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
></TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="xmlrpc-howto-php.html"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="section"
|
|
><H1
|
|
CLASS="section"
|
|
><A
|
|
NAME="xmlrpc-howto-java"
|
|
>9. Using XML-RPC with Java</A
|
|
></H1
|
|
><P
|
|
>Hannes Wallnöfer has provided <A
|
|
HREF="http://helma.at/hannes/xmlrpc/"
|
|
TARGET="_top"
|
|
>an excellent
|
|
implementation</A
|
|
> of XML-RPC for Java.</P
|
|
><P
|
|
>To install it, download the distribution, unzip it, and add the
|
|
<TT
|
|
CLASS="filename"
|
|
>*.jar</TT
|
|
> files to your <TT
|
|
CLASS="envar"
|
|
>CLASSPATH</TT
|
|
>. On a
|
|
Unix system, you can do this by typing:</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><PRE
|
|
CLASS="programlisting"
|
|
>bash$ unzip xmlrpc-java.zip
|
|
bash$ cd xmlrpc-java/lib
|
|
bash$ CLASSPATH=`pwd`/openxml-1.2.jar:`pwd`/xmlrpc.jar:$CLASSPATH</PRE
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><DIV
|
|
CLASS="section"
|
|
><H2
|
|
CLASS="section"
|
|
><A
|
|
NAME="xmlrpc-howto-java-client"
|
|
>9.1. A Java Client</A
|
|
></H2
|
|
><P
|
|
>Save the following program in a file named
|
|
<TT
|
|
CLASS="filename"
|
|
>JavaClient.java</TT
|
|
>.</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><PRE
|
|
CLASS="programlisting"
|
|
>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());
|
|
}
|
|
}
|
|
}</PRE
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><DIV
|
|
CLASS="section"
|
|
><H2
|
|
CLASS="section"
|
|
><A
|
|
NAME="xmlrpc-howto-java-server"
|
|
>9.2. A Stand-Alone Java Server</A
|
|
></H2
|
|
><P
|
|
>Save the following program in a file named
|
|
<TT
|
|
CLASS="filename"
|
|
>JavaServer.java</TT
|
|
>.</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><PRE
|
|
CLASS="programlisting"
|
|
>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());
|
|
}
|
|
}
|
|
}</PRE
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="xmlrpc-howto-c.html"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="index.html"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="xmlrpc-howto-php.html"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>Using XML-RPC with C and C++</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
> </TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Using XML-RPC with PHP</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |