Parsing the XMLRPC webservice response in Android

Featured Image

For the last few days I have been intensively playing with Android and Magento XMLRPC API. One of the trickiest things for me as a PHP developer was to figure out how to parse the data returned from XMLRPC API. Most of the time you will most likely have a proper documentation that will clearly outline the result data format. However, its nice to grasp some basic logic and learn a thing or two.

After bashing my head with several “hard” to parse result data from Magento API I decided to play a bit with figuring out the response format like there was not documentation. Below is a result of simple but practical way of figuring the returned result format.

//...
//(-1) Start Simple, as general object type
//Object customerGroupList = null;
 
//(0) Result of (1) after doing things based on (0)
Object[] customerGroupList = null;
 
try {
//(-1) Doing the web service call like (-1) data type
//customerGroupList = client.callEx("call", new Object[] { session, "customer_group.list"});
 
//(0) Doing the web service call like (0)
customerGroupList = (Object[])client.callEx("call", new Object[] { session, "customer_group.list"});
} catch (Exception e) {
Log.d("APP_INFO", "Exception: " + e.getMessage());
}
 
//(1) Check if the response isArray
//Log.i("APP_INFO", String.valueOf(customerGroupList.getClass().isArray()));
/**
* 06-14 17:34:40.141: INFO/APP_INFO(489): true
*/
 
//(2) If isArray, then iterate over it
for(Object o : customerGroupList) {
//(3) Get the type of object in the loop
Log.i("APP_INFO", o.getClass().getName());
/**
* 06-14 17:39:17.231: INFO/APP_INFO(515): java.util.HashMap
*/
 
//(4) Cast to appropriate object based on (3)
HashMap map = (HashMap)o;
 
//(5) Find out the fields/keys available in HashMap
Log.i("APP_INFO", "keys: " + map.keySet().toString());
/**
* 06-14 17:42:50.051: INFO/APP_INFO(541): keys: [customer_group_id, customer_group_code]
*/
 
//(6) Finally, play with the data :)
Log.i("APP_INFO", "Your data values are: 'customer_group_id'=> " + map.get("customer_group_id") + ", 'customer_group_code'=> " + map.get("customer_group_code"));
/**
* 06-14 17:46:14.231: INFO/APP_INFO(567): Your data values are: 'customer_group_id'=> 3, 'customer_group_code'=> Retailer
*/
}
//...

In the above example I have been using the open source Android-XMLRPC library to connect to Magento API.

Please keep in mind that above example is not copy-paste ready, it is extracted from other larger portion of my code. My intention was just to demonstrate how to work your way trough possible data parse glitches.

Unlike all forgiving PHP, Java is quite strict and it forces you to do proper and on time data type casting. Which is not an issue once you get the hang of it.

Hope that the above example gives you few ideas.

Cheers.

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


13 comments

  1. In orderList magento API it is allowed to pass filters.What type should this filters be?Hashmap or Object[]
    Object[] orders = (ArrayList)this.client.callEx(“call”, new Object[] { this.sessionId, “sales_order.list”, new HashMap ()})
    or
    Object[] orders = (ArrayList)this.client.callEx(“call”, new Object[] { this.sessionId, “sales_order.list”, new Object[]});

  2. Thanks for this great article.It saved a lot of time.Keep it coming, its thanks to guys like you that things go forward

  3. can i send image of bitmap or in JPEG format with the help of XML-RPC…?????
    if it is then pls answer…

  4. Is there a way like above to connect to the images , I am using :

    productsGroupList = (Object[])client.callEx(“call”, new Object[] { sessionId, “product_attribute_media.list”,”w123462″});

    but getting : Caused by java.lang.NullpointerException .

    Just trying to retrive a list of images

  5. Actually I have something in the Alpha/Beta stages.

    You can find it at http://magedroid.com

    It basically allows you to browse your catalog and get basic product information as well as some basic sales information.

    Please please please let me know your thoughts or suggestions. I am still working on it pretty hard and would love feedback as to what you think about it etc.

    Josh Pennington

  6. Heya Branko!
    I am just wondering, if there is something like a Administration-Backend-Viewer for Android yet. I’ve seen several posts on StackOverflow e.x. where the people asked for some connection things to the api and so on.
    But on the market there is nothing yet. I just know, that Magento itself release a “Magento Mobile” for Android and iPhone soon, where you can get a custimized FrontEnd for the users. But as far as I know, there is nothing for the Backend right now, just to take a look onto the stats, customers, whatever…
    Do you have got more information about that?

  7. Spent hours on trying to decipher the unknown object being returned and wondering why I couldn’t cast it as a hashtable. You saved me probably a few more hours, and a lot of slamming my head on the desk.

    Many thanks!

  8. Wow. It’s like you read my mind. I was having this exact same problem.

    Thanks!

  9. @Josh

    If you are using XMLRPC you might stumble on the “Cannot de-serialize nil” issue while trying to do web service call.

    If so, open the XMLRPCSerializer.java and add: “else if (typeNodeName.equals(TYPE_NIL)) { obj = parser.nextText(); } ” somewhere between those else if’s , then open the IXMLRPCSerializer.java and add the “String TYPE_NIL = “nil”;” on the list.

    This sees like a bug or incomplete class so, the above solution might fix the nil issues.

  10. I can’t thank you enough for this. I have been banging my head against a wall for days trying to figure this out.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.