Example XMLRPC communication between Django and Java


XMLRPC is useful when we need to send / receive information from one platform to others. In this case, I will use Django as the server which provide services that can be accessed as XMLRPC method from Java.

First, we need to setup Django projects and set the XMLRPC server. In quick way, I use “django-xmlrpc” and the source code can be taken from my Github https://github.com/yodiaditya/python-collection/tree/master/djangoserver. I have method called “test” which can be accessed through simple python script :

1
2
3
import xmlrpclib
s = xmlrpclib.ServerProxy("http://127.0.0.1:8000/xmlrpc/")
s.test(‘test’)

It will give result “test World!”.

Now how about in Java? There are several XMLRPC library, at this example I will use XMLRPC library from Apache.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.net.URL;

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

public class XmlRPCExample {

    public static void main( String args[] ) throws Exception {

       XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
       config.setServerURL(new URL("http://127.0.0.1:8000/xmlrpc/"));
       XmlRpcClient client = new XmlRpcClient();
       client.setConfig(config);
       Object[] params = new Object[]{new String("Test String")};
       String result = (String) client.execute("test", params);
   
       if (result != null) {
           System.out.println(result);
       }
    }
}

Now we can made connection through XMLRPC from Java to Django. Have fun! 😀

The full code can be taken from https://github.com/yodiaditya/java/tree/master/xmlrpc-client


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.