Testopia:Documentation:XMLRPC:Newer Client Example

From MozillaWiki
Jump to: navigation, search

The original example given uses the original version of xmlrpc, whereas only xmlrpc-2 might be available on your system. The following code worked for me on FC7. Note:

  • I stripped out all the SSL stuff and switched to using very basic Hashtables and Vectors
  • Your compiler will probably complain about using setBasicAuthentication as it is deprecated
  • If you get an error like "XmlRpcException: Login Required", you will need to edit your apache conf file per this. In my case I eliminated the AuthUserFile and Require valid-user.
  • If you are getting an HTTP 500 response, have a look at your apache logs and see if you haven't hit bug 396399. The workaround is given in discussion here
import org.apache.xmlrpc.XmlRpcClient;

import java.util.Hashtable;
import java.util.Vector;

public class RPCClient
{

    public static void main(String[] args)
    {
         try
        {


            XmlRpcClient client = new XmlRpcClient("http://bugzilla.mycompany.com/bugs/tr_xmlrpc.cgi");
            client.setBasicAuthentication("me@mycompany","password");

            Vector params = new Vector();
            params.add(new java.lang.Integer(2));

            Hashtable result = (Hashtable) client.execute("TestPlan.get", params);

            System.out.println(result);
        }
         catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}