Example on how to use XML RPC with Java

Hi all!!
I am trying to use XML RPC in a Java program…
for example…
if I wanted to know the hosts like the command onehost list does…
what should I do?

I look in the guide
look the onehost tab

and find the row

list top | one.hostpool.info | HOST:USE

then what should I do?

I’ve found the solution!!
I hope this help some newbye like me :smile:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package onetest;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
/**
 *
 * @author paolo
 */

public class OpenNebulaClient{

    private XmlRpcClient client = new XmlRpcClient();
    private XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
    private String session ="oneadmin:password";

    public OpenNebulaClient(String server_url){
        
       try{
           
           this.config.setServerURL(new URL(server_url));
           this.client.setConfig(this.config);
           
       }
       
       catch(MalformedURLException e){
           
           
           System.err.println(e);
           
       }
    }

    public void getUserInfo(int id) throws XmlRpcException{
        
        Object[] params = {session,id};
        
        try {
            
            
            Object[] result = (Object[])this.client.execute("one.user.info",params);

                if(!result[0].equals(null)) {
                    
                    boolean success = (Boolean)result[0];
                    String str_res = (String)result[1];
                    System.out.println("success="+success);
                    System.out.println("result="+str_res);
                }
                else{
                    System.out.println("success=0");
                }
        }
        catch (XmlRpcException e) {
            
            System.err.println(e);
            //e.printStackTrace();
        }
    }


    public static void main(String[] args){

        OpenNebulaClient on_client = new OpenNebulaClient("http://localhost:2633/RPC2");
        
        try{
            on_client.getUserInfo(0);
            
        }
        catch(XmlRpcException xmlrpce){
            
            System.err.println(xmlrpce);
        }
    }
}

Hi,

You may be also interested in the java bindings we provide:
http://docs.opennebula.org/4.14/integration/system_interfaces/java.html

1 Like

Thank you Carlos!

can I ask if there’s a way to use xml-rpc methods with curl command?
what is the xml equivalent of a command like “one.host.info”?

thank you

Hi,

I know for a fact that you can use curl to access an xmlrpc api, but I don’t have any examples for the OpenNebula api.

Regards