Xml remote procedure calls on android


SDK Version: 
M3

Using web services on android phones, is pretty simple. For most popular services, there is a usable library available.
Here is a little snippet for using an android library, called android-xmlrpc.

It's very small, a jar made of it's source is just 17 KByte in size.
Documentation is virtually non existent, or very bad. The whole topic is badly underdocumented.
For the past week, I have used trial-and-error tactics to find out what xml methods joomla, and other portal engines use. FYI they don't follow the official documentation.

  1. private XMLRPCClient mXMLRPCClient = new XMLRPCClient("web adress, the XML-RPC entry point");
  2.  
  3.         /**
  4.          * Do an xmlrpc call
  5.          *
  6.          * @param method method name
  7.          * @param values username/password etc
  8.          * @return object[] unparsed!
  9.          */
  10.         private Object getValue(String method, Object values[]) {
  11.                 // prepare variables
  12.                 Object results = null;
  13.  
  14.                 // generally, there are two kind of methods available, one with added values, one without.
  15.                 if (values == null) {
  16.                         try {
  17.                                 results = (Object) mXMLRPCClient.call(method);
  18.                                 return results;
  19.                         } catch (XMLRPCException e) {
  20.                                 throw new ConnectException();
  21.                         }
  22.                 } else {
  23.                         // do call
  24.                         try {
  25.                                 results = (Object) mXMLRPCClient.callEx(method, values);
  26.                                 return results;
  27.                         } catch (XMLRPCException e) {
  28.                                 //TODO
  29.                         }
  30.                 }
  31.         }
  32.  
  33. //usage:
  34. //
  35. //String method = "mt.getCategoryList";
  36. //Object[] toParse = (Object[]) getValue(method, new Object[]{"username","password"});
  37. //
  38. //

The received object could contain a lot of item types. Maybe it's a list, maybe it's just one item.
Here is an example, for parsing the received object(if it's just one item):

  1. /**
  2.  * Get post
  3.  *
  4.  * @param values[] String postid String username String password
  5.  *
  6.  */
  7. public void getPost(Object values[]) {
  8.         String method = "metaWeblog.getPost";           //name of the method
  9.         Object toParse = getValue(method, values);      //get values, put the result in an object
  10.         HashMap map = (HashMap) toParse;                //put the object into a hashmap, and get the values out by their keys
  11.        
  12.         if (map.get("userid") != null) {
  13.                 String storeIt = (String) map.get("userid");
  14.         }
  15. }

For more info read these pages:
http://www.xmlrpc.com/
http://www.sixapart.com/developers/xmlrpc/