xml rpc java vs ruby and the power of googling

Sun, Nov 4, 2007

I’ve been playing around with Zoto and Facebook, using Zoto’s XML-RPC service. They give a Python example on the developers’ page but I couldn’t get it to work, so I dug out some old Java XML-RPC code I had and tried that instead. It worked just fine. Then I had an idea. I did a quick Google, in fact I did 4 googles:

  • Ruby xmlrpc, which took me here.
  • Ruby associative array, which took me here.
  • Ruby variables, which took me here.
  • Ruby MD5, which took me here.
In about 5 mins I had googled enough information to allow me to write a Ruby Zoto client to extract my Zoto user information via XML-RPC. I had never written a line of Ruby before, ever! What’s more, the Ruby client was a fraction the size of the Java client. I’ve added the code for both at the end of this post.

I was so impressed with the ease of use of Ruby and the community support that it’s been sold to me. I will investigate Facebook/Zoto integration further, using Ruby of course ;)

Ruby port of the Python code on the Zoto site:

require ‘xmlrpc/client’ require ‘digest/md5’ require ‘pp’

If you want to access data that requires you to be logged in

then replace anonymous with your zoto credentials, otherwhise just

leave these variables set to anonymous.

zoto_username = “anonymous” zoto_password = “anonymous”

If you used your zoto credentials above, uncomment the following line.

zoto_password = Digest::MD5.hexdigest(zoto_password)

zoto_auth = {“username” => zoto_username, “password” => zoto_password} api_key = ‘Enter your 32 character api key here’

Establish cummunication with our server

zapi_server = XMLRPC::Client.new2(“http://www.zoto.com/RPC2/")

clints_awesome_info = zapi_server.call(“users.get_info “, api_key, zoto_auth, “clint”)

Print the return code

pp clints_awesome_info[0]

Print the user info

pp clints_awesome_info[1]

########################################################################

Java:

// ////////////////////////////////////////////////////////////////////////////////////////////////////////
package test.zoto;

import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcClient;

import java.net.URL; import java.util.HashMap;

public class ZotoTest { private static final String ZOTO_RPC_SERVER = “http://www.zoto.com/RPC2/"; private static final String API_KEY = “GET_AN_API_KEY”;

XmlRpcClientConfigImpl config = null; XmlRpcClient client = null;

public ZotoTest() throws Exception { config = new XmlRpcClientConfigImpl(); config.setServerURL(new URL(ZOTO_RPC_SERVER)); client = new XmlRpcClient(); client.setConfig(config); }

public void getUserInfo(String username, String password) { try { HashMap authCreds = new HashMap(); authCreds.put(“username”, username); authCreds.put(“password”, password);

  Object[] params = new Object[]{API_KEY, authCreds, username};

  Object[] result = (Object[])client.execute("users.get_info", params);

  System.out.println("Result code : " + result[0]);

  for (Object key : ((HashMap)(result[1])).keySet()) {
    System.out.println(key + " = " + ((HashMap)(result[1])).get(key));
  }
}
catch(Exception e) {
  System.out.println(e);
}

}

public static void main(String[] args) { try { ZotoTest zoto = new ZotoTest(); zoto.getUserInfo(“USERNAME”, “PASSWORD”); } catch(Exception e) { System.out.println(e); } } } // ////////////////////////////////////////////////////////////////////////////////////////////////////////

comments powered by Disqus