Java Reference
In-Depth Information
To call the remote method on the XML-RPC server, call the
XmlRpcClient
object's
exe-
cute(
String
,
Vector
)
object with two arguments:
The name of the method
n
The vector that holds the method's arguments
n
The name of the method should be specified without any parentheses or arguments. An
XML-RPC server usually documents the methods that it makes available to the public—
for example, there really is a
cadenhead.org
XML-RPC server that operates on port
4413 (it's my own test server). It offers
dmoz.getRandomSite()
, a method that returns an
Object
containing information about a random website. This method has no arguments.
The following statements create an XML-RPC client and call this method:
XmlRpcClient client = new XmlRpcClient(“http://cadenhead.org:4413”);
Vector params = new Vector();
Object result = client.execute(“dmoz.getRandomSite”, params);
The
execute()
method returns an
Object
that contains the response. This object should
be cast to one of the data types sent to a method as arguments:
Boolean
,
byte[]
,
Date
,
Double
,
Integer
,
String
,
Hashtable
, or
Vector
.
Like other networking methods in Java,
execute()
throws a
java.net.IOException
exception if an input/output error occurs during the connection between client and server.
There's also an
XmlRpcException
exception that is thrown if the server reports an XML-
RPC error.
Objects returned by the
execute()
method have the following data types:
Boolean
for
boolean
XML-RPC values,
byte[]
for
base64
data,
Date
for
dateTime.iso8601
data,
Double
for
double
values,
Integer
for
int
(or i4) values,
String
for
strings
,
Hashtable
for
struct
values, and
Vector
for arrays.
To see all this in a working program, enter the text of Listing 20.3 into your text editor
and save the file as
SiteClient.java
.
LISTING 20.3
The Full Text of
SiteClient.java
1: import java.io.*;
2: import java.util.*;
3: import java.net.*;
4: import org.apache.xmlrpc.*;
5:
6: public class SiteClient {
7: String url;