IOP GmbH originally ported the client portion of the Apache XML-RPC 1.0 library to the SuperWaba platform. Now you can bring your Enterprise Information System (EIS), like SAP, to your PDA. The xmlrpc4sw package is now maintained by Nimkathana as a sourceforge project at xmlrpc4sw.sourceforge.net. Check there for the latest version of this library.
This document shows you how to write a sample XML-RPC server and SuperWaba client application. You should already have a working Java environment for the server and a working SuperWaba environmet for the client set up. The platform used is Java 2 and SuperWaba 3.41 SDK.
This Article is based on an article from Kaare Digernes. Thank you!
The following sample application works like this:
The code for the server:
import java.io.IOException; import org.apache.xmlrpc.WebServer; import org.apache.xmlrpc.XmlRpc; public class HelloServer { public static void main(String args[]) { // Check if portnumber was supplied. if (args.length < 1) { System.out.println("Usage: java HelloServer [port]"); System.exit(-1); } try { // Use the Apache Xerces SAX driver. XmlRpc.setDriver("uk.co.wilson.xml.MinML"); // int port = Integer.parseInt(args[0]); int port = 9090; // Start the server. System.out.println("Server: starting XML-RPC server on port " + port ); WebServer server = new WebServer(port); // Register our handler class. server.addHandler("hello", new HelloHandler()); System.out.println("Server: registered HelloHandler class to 'hello'"); System.out.println("Server: now accepting requests..."); } catch (ClassNotFoundException e) { System.out.println("Server: could not locate Apache Xerces SAX driver."); } catch (IOException e) { System.out.println("Server: could not start server: " + e.getMessage()); } } // End of main(). } // End of class HelloServer.
The code for the handler:
import java.util.Vector; public class HelloHandler { HelloHandler() { System.out.println("Handler: default constructor called."); } public Vector sayHello() { Vector results = new Vector(); results.addElement("Hello, anonymous!"); return results; } public Vector sayHello(String name) { Vector results = new Vector(); results.addElement("Hello, " + name + "!"); return results; } } // End of class HelloHandler.
The code for the client; notice that the client will connect to the server localhost
on port 9090 using HTTP. If you run your server on a different port, you'll need to change that line accordingly.
import waba.util.*; import waba.ui.*; import com.nimkathana.swx.xmlrpc.XmlRpcClient; import com.nimkathana.swx.xmlrpc.XmlRpcException; public class XmlRpcTester extends MainWindow { public void reset() { lb.removeAll(); } private Button btn1; private Button btn2; private ListBox lb; private Edit ed; public XmlRpcTester() { super("XML-RPC Tester", TAB_ONLY_BORDER); } public void onStart() { add(btn1 = new Button(" Say Hello "), LEFT + 10, TOP + 2); add(btn2 = new Button(" Say Hello to you "), AFTER + 10, TOP + 2); add(new Label("Your name: ", LEFT), LEFT+10, AFTER + 2); add(ed = new Edit(""), AFTER + 5, SAME); ed.setText(""); add(new Label("", CENTER), LEFT, BOTTOM); add(lb = new ListBox()); lb.setRect(LEFT, AFTER + 2, FILL, FIT - 2, ed); } public void onEvent(Event e) { if (e.type == ControlEvent.PRESSED) { if (e.target == btn1) { reset(); doRemoteFunctionCall(false); } if (e.target == btn2) { reset(); doRemoteFunctionCall(true); } } } private void doRemoteFunctionCall(boolean withName) { try { // Specify which server to connect to. XmlRpcClient client = new XmlRpcClient("localhost", 9090, "/RPC2"); Vector parameters = new Vector(); Vector results = new Vector(); if (withName) { // Create a request to get the greeing when supplying a name. parameters = new Vector(); parameters.addElement(ed.getText()); results = (Vector) client.execute("hello.sayHello", parameters); lb.add("Client: response from the server: "); lb.add(results.elementAt(0)); } else { // Create a request to get the greeting without supplying a name. parameters = new Vector(); results = (Vector) client.execute("hello.sayHello", parameters); lb.add("Client: response from the server: "); lb.add(results.elementAt(0)); } } catch (XmlRpcException e) { lb.add("Client: XML-RPC exception: "); lb.add(e.getMessage()); } } }
This example uses port 9090 on localhost to run the server. Make sure you compile the handler class before the server class since the server depends on the handler. Be sure, that the library xlmrpc.jar is in your classpath.
> javac HelloHandler.java > javac HelloServer.java > java HelloServerThe server should run now:
> Server: starting XML-RPC server on port 9090 > Handler: default constructor called. > Server: registered HelloHandler class to 'hello' > Server: now accepting requests...
To run the client add the XML-RPC client library xmlrpc4sw-v0.2.jar
to your SuperWaba classpath und compile XmlRpcTester.java
. Start
the WabaApplet:
> java waba.applet.Applet /x 300 /y 100 /w 320 /h 320 /uiStyle PalmOS /bpp 8 /scale 1 XmlRpcTester
Now you can execute the two functions sayHello()
and sayHello(String
name)
on the XML-RPC server. The following picture shows the result of
the second function call.
The XML-RPC library is in an early development stage. But it is free software;
you can redistribute it and/or
modify it. The library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.