Consuming the Magento’s XMLRPC web service through Android

Consuming the Magento’s XMLRPC web service through Android

Recently I became proud owner of HTC Desire mobile device powered by Android platform. This is something I got my boss Tomislav Bilic to thank to. Even before (several weeks before) getting my real Android powered phone, I started poking around Android platform. Behind this interest was my desire is was/is to do mobile eCommerce apps. As I have been involved in everyday Magento development for the last two years now, it was only logical for my first steps to do something Magento-Android related.

So, what was on the plate? In order to do anything mobile with Magento you need to connect to its API, whether it is SOAP or XMLRPC API interface.

Personally I am a bigger fan of XMLRPC (can’t say why actually). In regards to that, my first task was to connect my Android to Magento XMLRPC API. Surprisingly, Android framework (SDK) does not have feature rich and ready classes for doin XMLRPC calls like lets say Zend Framework has.

Luckily for me, there was already someone who had to deal with this issue before me. If you open the Android-XMLRPC page you will find a nice little library for handling the XMLRPC in Android. This was all I needed to get me started. Here is an example of how to connect to Magento trough this library.


// ...

String sessionId = "";

//HashMap<string , String> params = new HashMap</string><string , String>();
//params.put("apiUser", "developer");
//params.put("apiKey", "magento123");

XMLRPCClient client = new XMLRPCClient("http://some-site.com/index.php/api/xmlrpc/");

try {
	/*
		sessionId = (String)client.callEx("login", new Object[]{params});
		will cause
		DEBUG/MY_XMLRPCException_MSG(196): XMLRPC Fault: Calling parameters do not match signature 1
	*/
	sessionId = (String)client.call("login", "developer", "magento123");

	Log.d("MY_XMLRPC_SUCCESS_SESSION_ID", sessionId);
}
catch (XMLRPCException e) {

	Log.d("MY_XMLRPCException_MSG", e.getMessage());
}

// ...

By executing above you will most likely trigger the exception and get the following error.

/*
06-09 09:33:53.313: DEBUG/MY_XMLRPCException_MSG(226): java.net.SocketException: Permission denied (maybe missing INTERNET permission).
*/

As you can see, error is pretty self-descriptive. You need to open your AndroidManifest.xml file and add the following inside the "manifest" attribute.

/*
<uses -permission android:name="android.permission.INTERNET"></uses>
*/

After successfully executed XMLRPC request, you should see the log message like shown below.

/*
06-09 09:49:27.163: DEBUG/MY_XMLRPC_SUCCESS_SESSION_ID(223): 9273ad9a5a52a959fec32439944bc84b
*/

Meaning you now have session id and can do the rest of the Magento XMLRPC method calls by passing this session id.

Also, you can see in the code, that I commented some stuff. For those ambitions this is a little tip on how to do more complex API calls to the rest of the Magento XMLRPC API methods exposed. This one you need to figure on your own.

Hope it comes in handy to the Magento-Android newcomers.

From Croatia with love, cheers.

14
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Branko is Inchoo's CTO with over 3 years of active / everyday full time Magento development.

Other posts from this author

Discussion 14 Comments

Add Comment
  1. This is really a great work.

    Thanks!

  2. Great work.I Like it.

    But I have one question in this sentence
    Consuming the Magento’s XMLRPC web service “trough” Android

    Here “trough” is intentionally or mistake.

    Thanks

  3. Hey Vijay. THanks for pointing it out. The typo is now fixed. Feel free to let us know if you notice anything else. English is not our native language :)

  4. Josh Pennington

    Thanks for the tutorial.

    Could you also maybe do another basic call. Something like sales_order.list too?

    I am very interested to see how a call like that would be put together?

  5. Thanks a lot for this little tutorial!

    I have just one correction about the internet permission, the right code is:

    (no space between “uses” and “-permission ” and autoclose tag.

  6. arg, XML has disappeared from my previous comment…

  7. maqish

    I was wondering, i’ve managed so far to retrieve information from magento and show this in a table using my android. it also shows a nice image.

    Now there is this filter. Has anyone already tried to use the filter with xmlrpc in java?

    if so can someone please post an example?

    greetings and thanks in advance Marc

  8. Madhukar

    Hi,

    i am new to magento and looking to publish api for catalog search is there any code readily available?

    Thanks in advance

  9. Josh

    I am having trouble setting up android-xmlrpc, I get an error right away when I import the project into eclipse:

    Description Resource Path Location Type
    Project has no default.properties file! Edit the project properties to set one. XMLRPC Unknown Android Target Problem

    Description Resource Path Location Type
    The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project XMLRPC Unknown Java Problem

    Description Resource Path Location Type
    The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files XMLRPCServer.java /XMLRPC/src/org/xmlrpc/android line 1 Java Problem

    Those 3 different error messages. How do I setup android-xmlrpc correctly?

  10. Josh

    nevermind, for those of you who have this same issue in the future, you must create a new project from source in eclipse rather than using the import project feature

  11. Hello,

    Thanks a lot for your tutorial.

    I have a question about the calls to the rest of the Magento XMLRPC API methods because I don’t know where I should put the session id parameter.

    Could you show an example of calling any method or for example the customer.create??

    Thanks in advance

  12. daragh

    I can’t get the XMLRPC to intergrate with eclipse. any good tuts for this ?. Eclipse keeps underlining all the code in red.

  13. Albert

    Hi there,

    Great tuto.
    I’m testing it and I get the session correctly but It’s impossible for me to get any other call working.

    Can you post an example to see how is suposed to put the sessionid in that call?

    I’m really blocked right now

    Thanks!!

  14. Hanush H Nair

    @daragh
    Did u put the required libraries?

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top