Blakes 21 Days Chapter 20 Document
Day 20, XML Web Services
- Top
- 549 Over the years, numerous attempts have been made to create a standard protocol for remote procedure calls (RPC).
- These are a way for one computer program to call a procedure in another program over a network such as the Internet.
- 549 Often, these protocols are completely language-agnostic.
- This allows a client program written in a language such as C++
- to call a remote database server written in Java or something else
- without either side knowing (or caring) about its partner's implementation language.
- 549 Web services - networking programs that use the Web to offer data in a form easily digested by other software -
- are being employed to share account authentication between sites, facilitate e-commerce transactions between stores,
- provide business-to-business information exchange, and other innovative offerings
- 549 A simple, useful example of this idea is XML-RPC,
- a protocol for using Hypertext Transfer Protocol (HTTP) and Extensible Markup Language (XML) for remote procedure calls.
- Today, you learn how to implement it in Java as the following topics are covered:
- How to communicate with another computer using XML-RPC
- How to structure an XML-RPC request and an XML-RPC response
- How to use XML-RPC in Java programs
- How to send an XML-RPC request
- How to receive an XML-RPC response
Introduction to XML-RPC
- Top
- 550 Java supports one well-established technique for remote procedure calling: remote method invocation (RMI).
- 550 RMI is designed to be a complex, robust solution to a large variety of remote computing tasks.
- This sophistication has been one of the hindrances to the adoption of RPC.
- The complexity required to implement some of these solutions can be more than a programmer wants to take on simply to exchange information over a network.
- 550 A much simpler alternative, XML-RPC, has become adopted for web services.
- 550 Client/server implementations of XML-RPC are available for most platforms and programming language in widespread use.
- 550 XML-RPC exchanges information using a combination of HTTP, the protocol of the Web, and XML.
- 550 XML-RPC supports the following data types:
- array - A data structure that holds multiple elements of any of the other data types, including arrays
- base64 - Binary data in Base 64 format
- boolean - True-false values that are either 1 (true) or 0 (false)
- dateTime.iso8601 - A string containing the date and time in ISO 8601 format,
- such as 20150927T12:01:15 for 12:01 a.m. (amd 15 seconds) on September 27, 2015
- double - 8-byte signed floating-point numbers
- int (also called i4) - Signed integers ranging in value from -2,147,483,648 to 2,147,483,647, the same size as int values in Java
- string - Text
- struct - Name-value pairs of associated data where the name is a string
- and the value can be any of the other data types (comparable to the HashMap class in Java)
- Top
- 550 One thing noticeably absent from XML-RPC is a way to request data as an object.
- The protocol wasn't designed with object-oriented programming in mind, but you can represent reasonably complex objects with the array and struct types.
- 550 By design, XML-RPC is a simple protocol for programming across the network.
- The protocol has been implemented on web services running Windows, Macintosh, and Linux systems
- Top
- 551 After the release of XML-RPC, the specification was extended to create another RPC protocol called Simple Object Access Protocol (SOAP).
- 551 SOAP shares design goals of XML-RPC and has been expanded to better support objects, user-defined data types, and other advanced features, resulting in a significantly more complex protocol.
- SOAP also has become widely popular for web services and other decentralized network programming.
- 551 NOTE: Because SOAP is an extension of XML-RPC, it raises the question of why the latter protocol is still in use.
- When SOAP came out and was considerably more complex than XML-RPC, some programmers choose to stick with the simpler protocol.
- To find out more about SOAP and public servers that can be used with SOAP clients, visit the website www.xmethods.com.
- It looks like the link to www.xmethods.com does not work.
Communicating with XML-RPC
- Top
- 551 XML-RPC is a protocol transmitted via HTTP, the standard for data exchange between web servers and web browsers.
- The information it transmits is not web pages.
- Instead, it is XML data encoded in a specific way.
- 551 Two kinds of data exchanges are conducted using XML-RPC:
- client requests and server responses.
Sending a Request
- Top
- 551 An XML-RPC request is XML data sent to a web server as part of an HTTP post request.
- 551 A post request normally is used to transmit data from a web browser to a web server.
- Java servlets, CGI programs, and other software collect the data from a post request and send back HTML in response.
- When you submit an email from a web page or vote in an online poll, you're using either post or a similar HTTP request called get.
- 552 XML-RPC simply uses HTTP as a convenient protocol for communicating with a server and receiving a response.
- 552 The request consists of two parts:
- the HTTP headers required by the post transmission, and
- the XML-RPC request, which is expressed as XML.
- 552 Listing 20.1 is an example of an XML-RPC request.
Listing 20.1 An XML-RPC Request
- Top
- page 552
POST /XMLRPC HTTP/1.0
Host: www.advogato.org
Connection: Close
Content-Type: text/xml
Content-Length: 151
User-Agent: OSE/XML-RPC
<?xml version="1.0"?>
<methodCall>
<methodName>test.square</methodName>
<params>
<param>
<value>
<int>13</int>
</value>
</param>
</params>
</methodCall>
The Explanation
- Top
- 552 In Listing 20.1, lines 1-6 are the HTTP headers, and lines 8-18 are the XML-RPC request.
- This listing tells you the following
- The XML-RPC server is at www.advogato.org/XMLRPC (lines 1 and 2).
- The remote method being called is test.square (line 10).
- The method is being called with one argument, an integer with a value of 13 (lines 12-16).
- 552 Unlike their counterparts in Java, method names in an XML-RPC request do not include parentheses.
- They consist of the name of an object followed by a period and the name of the method, or simply the name of the method, depending on the XML-RPC server.
- 553 CAUTION: XML-RPC, which has been implemented in numerous computer-programming languages,
- has differences in terminology from Java:
- Methods are called procedures, and method arguments are called parameters.
- The Java terms are used often during today's lesson when Java programming techniques are discussed.
Responding to a Request
- Top
- 553 An XML-RPC response is XML data sent back from a web server like any other HTTP response.
- Again, XML-RPC piggybacks on an established process - a web server sending data via HTTP to a web browser - and uses it in a new way.
- 553 The response also consists of HTTP headers and an XML-RPC response in XML format.
- 553 Listing 20.2 is an example of an XML-RPC response.
Listing 20.2 An XML-RPC Response
- Top
- page 553
HTTP/1.0 200 OK
Date: Sat, 27 Sep 2015 04:44:13 GMT
Server: Apache/2.2.3 (CentOS)
ETag: "PbT9cMgXsXnw520qREPNAA=="
Content-MDS: PbT9cMgXsXnw520qREPNAA==
Content-Length: 157
Connection: Close
Content-Type: text/xml
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<int>169</int>
</value>
</param>
</params>
</methodCall>
The Explanation
- Top
- 553 In Listing 20.2, lines 1 - 8 are the HTTP headers, and lines 10 - 19 are the XML-RPC response.
- You can learn the following things from this listing:
- The response is 157 bytes in size and is in XML format (lines 6 and 8).
- The value returned by the remote method is an integer that equals 169 (line 15).
- 554 An XML-RPC response contains only one argument, contrary to what you might expect from the parses tag in line 12.
- If the remote method does not return a value - for example, it might be a Java method that returns void - an XML-RPC server still returns something.
- 554 This return value can be primitive data, strings, arrays of varying dimensions, and more sophisticated data structure such as key/value pairs
- (the kind of thing you could implement in Java using HashMap).
- Top
- 554 NOTE: The XML-RPC request and response examples were generated by a server run by the Advogato open source advocacy site.
- You can find out more about XML-RPC server at www.advogato.org/xmirpc.html
- Several XML-RPC debuggers on the web can be used to call remote methods, which makes it much easier to determine if a client or server is working correctly.
Choosing an XML-RPC Implementation
- Top
- 554 Although you can work with XML-RPC by creating your own classes to read and write XML and
- exchange data over the Internet, an easier route is to use a preexisting Java Class Library that supports XML-RPC.
- 554 One of the most popular is Apche XML-RPC,
- an open source project managed by the developers of the Apache web server,
- Tomcat Java servlet engine and other popular open source software.
- 554 The Apache XML-RPC project, which consists of the org.apache.xmlrpc package and three related packages,
- contains classes that can be used to implement an XML-RPC client and server with a small amount of your own code.
- 554 The project has a home page at the web address http://xml.apache.org/xmlrpc.
- To use this project, you must download and install it.
- 554 Apache XML-RPC can be downloaded as either a ZIP archive or TAR.GZ archive.
- Top
- 555 CAUTION: If you have trouble downloading Apache XML-RPC from the Apache website, you can get it from the book's website.
- Visit www.java21days.com, go to the page for Day 20 and click the :Apache XML-RPC Library version 3.1.3: link.
- This is a ZIP archive containing all the project's files.
- The software is open source and can be shared under the Apache License.
- 555 Dowbload the library and extract the files on a folder on your computer.
- When that's done, follow these steps to add Apache XML-RPC to NetBeans:
- Choose Tools, Libraries. The Ant Library Manager opens.
- Click New Library. The New Library dialog appears.
- Enter Apache XML-RPC 3.1.3 as the Library Name, and click OK.
- Back in the Library Manager, click Add JAR/Folder.
- Browse to the folder where you extracted the archive, and open it.
- Open the lib subfolder and choose all five JAR files it contains
- commons-logging-1.1.jar
- ws-commons-util-1.0.2.jar
- xmlrpc-client-3.1.3.jar
- xmlrpc-common-3.1.3.jar
- xmlrpc-server-3.1.3.jar
- Click Add JAR/Folder
- Back in the Library Manager, click OK.
- Top
- 555 After you have added the library to NetBeans, you need to add it to the current project so that you can use the Apache XML-RPC classes in today's programs:
- In the Projects pane, look for the folder named Libraries below the .java files for the classes you have created.
- Right-click the Libraries folder and choose Add Library. The Add Library dialog appears.
- Choose Apache XML-RPC 3.1.3, and click OK.
- 555 The five JAR files composing this class library appear under Libraries in the pane.
- 555 After the library is set up, an import statement makes it easy to refer to the classes in a package, as in this example:
- import org.apache.xmlrpc.*;
- 556 This makes it possible to refer to the classes in the main package, org.apache.xmlrpc, without using the full package name.
- You'll work with this package in the next two sections.
Using an XML-RPC Web Service
- Top
- 556 An XML-RPC client is a program that connects to a server, calls a method on a program on that server, and stores the result.
- 556 Using Apache XML-RPC, the process is comparable to calling any other method in Java.
- You don't have to create an XML request, parse an XML response, or connect to the server using one of Java's networking classes.
- 556 In the org.apache.xmlrpc.client package, the XmlRpcClient class represents a client.
- The client is set up with the XmlRpcClientConfigImpl class, which holds the configuration settings for the client.
- 556 The server is set by calling the configuration object's setServerURL(URL) method with a URL object that contains the server's address and port number.
- 556 After configuration is complete, the client's setConfig() method is called with that configuration as the only argument.
- 556 The following statements create a client to an XML-RPC client on the host cadenhead.org at the port 4413:
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
URL server = new URL("http://cadenhead.org:4413/");
config.setServerURL(server);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
- 556 If you are calling a remote method with any arguments, they should be stored in an ArrayList object, a data structure that holds objects of different classes.
- 556 To work with array lists, call the ArrayList() constructor with no arguments, and call its add(Object) method with each object that should be added to the list.
- Objects can be of any class and must be added to the list in the order in which they are called in the remote method.
- 557 The following data types can be arguments to a remote method:
- byte[] arrays for base64 data
- Boolean objects for boolean data
- Date objects for dateTime.iso8601 values
- Double objects for double values
- Integer objects for int values
- String objects for string values
- HashMap objects for struct values
- ArrayLists objects for arrays
- 557 The Date, HashMap, and ArrayList class are in the java.util package.
- 557 For example, if an XML-RPC server has a method that takes String and Double arguments,
- the following code creates an array list that holds each argument:
String code = "conical";
Double xValue = new Double(175);
ArrayList parameters = new ArrayList();
parameters.add(code);
parameters.add(xValue);
- 557 To call the remote method on the XML-RPC server, call the XmlRpcClient object's execute() method with two arguments:
- The name of the method
- The array list that holds the method's arguments
- 557 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.
- Top
- 557 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, HashMap, or ArrayList.
- 557 Like other networking methods in Java, execute() throws an XmlRcpException exception if the server reports an XML-RPC error.
- 557 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, HashMap for struct values, and ArrayList for arrays.
- Top
- 558 To see all this in a working program, enter the code shown in Listing 20.3 into the BetBeans editor as the class SiteClient in the package com.java21days.
Listing 20.3 - The Full Text of SiteClient.java
package com.java21days;
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.client.*;
public class SiteClient {
public static void main(String arguments[]) {
SiteClient client = new SiteClient();
try {
HashMap resp = client.getRandomSite();
// Report the results
if (resp.size() > 0) {
System.out.println("URL: " + resp.get("url")
+ "\nTitle: " + resp.get("title")
+ "\nDescription: " + resp.get("description"));
}
} catch (IOException ioe) {
System.out.println("Exception: " + ioe.getMessage());
} catch (XmlRpcException xre) {
System.out.println("Exception: " + xre.getMessage());
}
}
public HashMap getRandomSite()
throws IOException, XmlRpcException {
// Create the client
XmlRpcClientConfigImpl config = new
XmlRpcClientConfigImpl();
URL server = new URL("http://localhost:4413/");
config.setServerURL(server);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
// Create the parameters for the request
ArrayList params = new ArrayList();
// Send the request and get the response
HashMap result = (HashMap) client.execute(
"dmoz.getRandomSite", params);
return result;
}
}
The Explanation
- Top
- 559 The SiteClient application connects to the XML-RPC server and calls the dmoz.getRandomSite() method on the server with no arguments.
- When it works, this method returns a HashMap that contains the site's URL. title, and description in strings with the keys "url", "title", and "description".
- 559 This class can be run, but it won't work because the XML-RPC server hasn't been implemented yet.
- Top
- 559 NOTE: These random sites are culled from the database of the Open Directory Project, a directory of more than five million sites at www.dmoz.org.
- The project's data is available for redistribution by others at no cost under the terms of the Open Directory License.
- For more information, visit www.dmoz.org/help/getdata.html.
- As of Mar 17, 2017, dmoz.org is no longer available.
- Here's a mirrow static site.
Creating an XML-RPC Web Service
- Top
- 559 An XML-RPC server is a program that receives a request from a client, calls a method in response to that request, and returns the result.
- The server maintains a list of methods that it allows clients to call; these are different Java classes called handlers.
- 559 Apache XML-RPC handles all the XML and networking itself, enabling you to focus on the task you want a remote method to accomplish.
- 559 There are several ways to serve methods remotely.
- The simplest is to use the WebServer class in the org.apache.xmlrpc.webserver package,
- which represents a simple HTTP web server that response to only XML-RPC requests.
- 559 This class has two constructors:
- WebServer\(int) creates a web server listening on the specified port number.
- WebServer(int, InetAddress) creates a web server at the specified port and IP address
- The second argument is an object of the java.net.InetAddress class.
- 559 Both constructors throw IOException exception if an input/output problem occurs with creating and starting the server.
- 559 The web server has an XmlRpcServer object associated with it that handles tasks related to the protocol.
- This class is in another package, org.apache.xmlrpc.server.
- Call the web server's getXmlRpcServer() method with no arguments to retrieve it.
- 560 The following statements create a web server on port 4413 and an object for its XML-RPC server:
WebServer server = new WebServer(4413);
XmlRpcServer xmlRpcServer - server.getXml
- Top
- 560 The web server does not contain the remote methods that clients call via XML-RPC.
- These reside in handlers.
- 560 Handlers are set up by another class in the org.apache.xmlrpc.server package, PropertyHandlerMapping.
- This class contains configuration settings for an XML-RPC server,
- which can be set with a properties file or by calling its methods.
- It can be created with no arguments to the constructor:
PropertyHandlerMapping phm = new PropertyHandlerMapping();
- 560 To add a handler, call the mapping object's addHandler(String, Object) method with two arguments.
- Top
- 560 The first argument to addHandler() is a name to give the handler, which can be anything you choose.
- Naming an XML-RPC method is comparable to naming a variable.
- Clients will use this name when calling remote methods.
- 560 The SiteClient application created earlier today called the remote method dmoz.getRandomSite().
- The first part of this call - the text preceding the period - refers to a handler given the name dmoz.
- Top
- 560 The second argument to addHandler() is a Class object for the handler's class.
- 560 These statements add a handler named dmoz to the XML-RPC server's property mapping and then set the server to use that configuration:
phm.addHandler("dmoz", DmozHandlerImpl.class);
xmlRpcServer.setHandlerMapping(phm);
- 560 The DmozHandlerImpl class is the one that implements the getRandomSite() method and any others that can be called remotely over XML-RPC.
- You'll create this class in a moment.
- 560 A class that handles remote method calls can be any Java class that contains public methods that return a value,
- as long as the methods take arguments that correspond with data types supported by Apche XML-RPC:
- Boolean, byte[], Date, double, HashMap, int, String, and ArrayList.
- 561 You can put existing Java classes to use as XML-RPC handles without modification
- as long as they do not contain public methods that should not be called and each public method returns a suitable value.
- Top
- 561 CAUTION: The suitability of return values relates to the Apache XML-RPC implementation rather than XML-RPC itself.
- Other implementations of the protocol are likely to have some differences in the data types of the arguments
- they take in remote methods calls and the values they return.
- Top
- 561 Using Apache XML-RPC, the web server allows any public method in the handler to be called,
- so you should use access control to keep prying clients out of methods that should remain off limits.
- 561 As the first step toward creating an XML-RPC service, the following code creates a simple web server that takes XML-RPC requests.
- In NetBeans, use Listing 20.4 to create the DmozServer application.
Listing 20.4 The Full Text of DmozServer.java
package com.java21days;
import java.io.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.server.*;
import org.apache.xmlrpc.webserver.*;
public class DmozServer {
public static void main(String[] arguments) {
try {
startServer();
} catch (IOException ioe) {
System.out.println("Server error: " +
ioe.getMessage());
} catch (XmlRpcException xre) {
System.out.println("XML-RPC error: " +
xre.getMessage());
}
}
public static void startServer() throws IOException,
XmlRpcException {
// Create the server
System.out.println("Starting Dmoz server ...");
WebServer server = new WebServer(4413);
XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
// Register the handler
phm.addHandler("dmoz", DmozHandlerImpl.class);
xmlRpcServer.setHandlerMapping(phm);
// Start the server
server.start();
System.out.println("Accepting requests ...");
}
}
The Explanation
- Top
- 562 This class can't be compiled successfully until you have created the handler class DmozHandlerImpl and a DmozHandler interface that it implements.
- 562 The DmozServer application creates a web server at port 4413 and an associated XML-RPC server in lines 26-27.
- 562 Using the server's property mapping, a handler is added to the server: a DmozHandlerImpl object given the name "dmoz".
- The server's start() method is called to begin listening for requests.
- 562 That's all the code required to implement a functional XML-RPC server.
- Most of the work is in the remote methods you want a client to call.
- They don't require any special techniques as long as they are public and return a suitable value.
- 562 To give you a complete example you can test and modify to suit your own needs,
- the DmozHandler interface and DmozHandlerImpl class are provided in the next two listings.
- 562 The DmozHandler interface defines the public methods that can be called remotely over XML-RPC.
- Create a new empty Java file of this class name, aanf fill it with Listing 20.5.
Listing 20.5 The Full Text of DmozHandler.java
- Top
- Page 562
package com.java21days;
import java.util.*;
public interface DmozHandler (
public HashMap getRandomSite();
}
- Top
- 563 This interface contains one method, getRandomSite(), which returns a HashMap.
- No other methods can be called.
- 563 The DmozHandlerImpl class is an implementation of the DmozHandler interface.
- 563 The techniques employed in this class were covered during Day 18, "Accessing Database with JDBC 4.2 and Derby."
- They are a good review of how to use JDBC to retrieve records from a database - in this example, a MySQL database called cool.
- 563 Enter the code shown in Listing 20.6 in NetBeans as the class DmozHandlerImpl.
Listing 20.6 The Full Text of DmozHandlerImpl.java
package com.java21days;
import java.sql.*;
import java.util.*;
public class DmozHandlerImpl implements DmozHandler {
public HashMap getRandomSite() {
Connection conn = getMySqlConnection();
HashMap response = new HashMap<>();
try {
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery(
"SELECT * FROM cooldata ORDER BY RAND() LIMIT 1");
if (rec.next()) {
response.put("url", rec.getString("url"));
response.put("title", rec.getString("title"));
response.put("description",
rec.getString("description"));
} else {
response.put("error", "no database record found");
}
st.close();
rec.close();
conn.close();
} catch (SQLException sqe) {
response.put("error", sqe.getMessage());
}
return response;
}
private Connection getMySqlConnection() {
Connection conn = null;
String data = "jdbc:mysql://localhost/cool";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
data, "user", "d05eURCe");
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}
return conn;
}
}
The Explanantion
- Top
- 564 Lines 34-38 of the DmozHandlerImpl application should be changed to reflect your own database, username, and password.
- In this class, a MySQL database named cool is accessed on the local computer with the username "cool" and the password "mrfreeze".
- You also might need to change the rest of the string used to connect to the database, depending on your driver.
- 564 When the server is up and running, you can run SiteClient to see the data from a randomly selected website, as shown in Figure 20.1
Figure 20.1 - Receiving Dmoz website data over XML-RPC. - goes here
- Top
- 564 NOTE: Running this particular XML-RPC server also requires a database.
- To download a MySQL database containing information on 1,000 websites from the Open Directory Project,
- visit this book's website at www.java21days.com and open the Day 20 page.
- The database is in a file named dmozdata.dat and is a text file of SQL commands that can be used to create the database on a MySQL server.
Summary
- Top
- 565 XML - RPC has been described as the "lowest common denominator" of remote procedure call protocols,
- but this isn't considered an insult by its originators.
- Most attempts to facilitate software communication over a network have been sophisticated,
- scaring off developers who have simpler needs.
- 565 The XML - RPC protocol can be used to exchange information with any software that supports HTTP,
- the lingua franca of the Web, and XML, a highly popular, structured format for data.
- 565 By looking at XML - RPC requests and responses,
- you should be able to figure out how to use the protocol even without reading the protocol specification.
- 565 However, as implementations such as Apache XML-RPC became more extensive,
- you can begin using it quickly without ever looking at the protocol.
Q & A
- Top | page 565
- Q 565 When I try to return a as String array from a remote method, Apache XML-RPC responds with an XmlRpcException that states that the object is not supported. Which objects does it support ?
- A 565 Apache XML-RPC returns 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
- HashMap for struct values
- or ArrayList for arrays
- 565 These are specific to Apache XML-RPC.
- Other class libraries that support this format may work with different data types and classes in Java.
- Consult the documentation for those libraries.
- Q 565 I'm writing an XML-RPC client to call a method that returns binary data ( base64, in other words). The execute() methods of XmlRpcClient returns an object instead of an array of bytes. How do I convert this ?
- A 565 Arrays are objects in Java, so you can use casting to convert the object returned by execute() to an array of bytes (assuming that the object really is an array).
- 565 The following statement accomplishes this on an object named fromServer that contains a byte array:
- byte[] data = (byte[]) fromServer;
Quiz - Questions
- Top | page 566 | Review today's material by taking this three-question quiz.
- Which popular Internet protocol does XML-RPC not require ?
- HTML
- HTTP
- XML
- Which XML-RPC data type would be best suited to hold the number 8.67 ?
- boolean
- double
- int
- Which XML tag indicates that the data is an XML-RPC request ?
- methodCall
- methodResponse
- params
Answers
- Top | Note A is 1, B is 2, and C is 3
- A. XML-RPC uses HTTP (Hypertext Transfer Protocol) to transport data that is formatted as XML (Extensible Markup Language).
- HTML Hypertext Markup Language) is not used.
- B. All floating-point numbers such as 8.67 are represented by the double type in XML-RPC.
- There are not two different floating-point types, as there are in Java (float and double).
- A. The methodCall tag is used only in requests, methodResponse is used only in responses, and params is used in both.
Certification Practice
- Top | page 566
- 566 The following question is the kind of thing you could expect to be asked on a Java programming certification test. Answer it without looking at today's material or using the Java compiler to test the code.
- The answer is available on the book's website at www.java21days.com
- Given:
public class Operation {
public static void main(String[] arguments) { i = 40;
int x = 1;
int y = 3;
if ((x != 1) && (y++ == 3)) {
y = y +2;
}
}
}
- What is the final value of y ?
- 3
- 4
- 5
- 6
Exercise
- Top | page 567
- To extend your knowledge of the subjects covered today, try the following exercises:
- The programming site Advogato offers an XML-RPC interface to read member dairies at http://www.advogato.org/xmlrpc.html
- Write an application that reads a member's last 10 diary entries.
- The
XML-RPC interface for the weblog update service Weblogs.com is at www.weblogs.com/api.html
- Write a client and server that can send and receive the weblogUpdate.ping method.