10

How to get the details of Multiple products in a Single Call in Android using XMLRPC from Magento.I am able to get the list of products using the function catalog_product.list using XMLRPC.

Now, i have the SKU id's of all the products.I am able to get the media details of each product using the function product_media.list.

If suppose I have 10 products,i have to call 10 times product_media.list method for each product which takes long time.

So,how can I call the multiCall function of Magento in Android. Many tutorials in php for calling the multiCall function are posted but I am not able to imitate the same in Android.

So please help me if you have similar code snippet that can make me understand multiCall function(for Android) so that I can Advance further using that.
Thanks.


PHP code Example from Josua Marcel C 's Answer:


$session = $client->call('login', array('apiUser', 'apiKey'));
$client->call('call', array($session,'somestuff.method', array('arg1', 'arg2', 'arg3')));  
$client->call('call', array($session, 'somestuff.method', 'arg1'));   
$client->call('call', array($session, 'somestuff.method'));

$client->call('multiCall', 
               array($session,
                  array(
                      array('somestuff.method', 'arg1'),
                      array('somestuff.method', array('arg1', 'arg2')),
                      array('somestuff.method')
                   )
              )
            );  

I would like to imitate the above php code in Android that calls the multiCall() function of the Magento.


Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
  • Is there an XML-RPC library you've used to call catalog_product.list and product_media.list? Can you talk about what you've tried already with multiCall? – Iain Aug 20 '12 at 05:49
  • @lain Yes,I am using XML-RPC library for calling every lists and other data.I have tried many vivid ways my self but failed every time.And to the worst,there is no single example I have found in my 10 days research that would give me a hint to use the multiCall() method in Android. – Haresh Chaudhary Aug 20 '12 at 05:52
  • 1
    If I am using simply a call() method then I am able to fetch every thing.But as you may know that using call method for even 5 calls takes more time.And the main advantage of multiCall() method is that it would take "all the calls of the client" in a its single call and bring me a single result. – Haresh Chaudhary Aug 20 '12 at 05:55
  • Which XML-RPC library are you using? – Iain Aug 20 '12 at 08:08
  • @laim It's was for Android--http://code.google.com/p/android-xmlrpc/ – Haresh Chaudhary Aug 20 '12 at 08:11

3 Answers3

3

After making long long Research, I got half-way Solution that calls the multiCall() method without any Error,but Still I don't know how to get the response of the Server in a variable and use it.

AnyOne who has knowledge of it can Edit my Answer, I will be thankful to him.

The Code that I have Used is :

Object[] skuid=new Object[product_list.size()];
Object calling[]=new Object[product_list.size()];

for(int m=0;m<product_list.size();m++)
{
    skuid[m]=new Object[]{product_list.get(m).getp_Sku()};
    calling[m]=new Object[]{"catalog_product_attribute_media.list",skuid[m]};   
}

try 
{
  client.callEx("multiCall",new Object[]{Utils.sessionId,calling});
}
catch (XMLRPCException e) 
{
    e.printStackTrace();
}  

AcknowledgeMents :

I have worked on the Answer posted by Iain.

Community
  • 1
  • 1
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
2

The Answer

since android is based java application, You can use this.

package org.apache.xmlrpc;

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

public class MultiCall
implements ContextXmlRpcHandler
{
    public Object execute(String method, Vector params, XmlRpcContext context)
            throws Exception
    {
        if ("multicall".equals(method))
        {
            return multicall(params, context);
        }

        throw new NoSuchMethodException("No method '" + method + "' in " + this.getClass().getName());
    }

    public Vector multicall(Vector requests, XmlRpcContext context)
    {
        // The array of calls is passed as a single parameter of type array.
        requests=(Vector)requests.elementAt(0);
        Vector response = new Vector();
        XmlRpcServerRequest request;
        for (int i = 0; i < requests.size(); i++)
        {
            try
            {
                Hashtable call = (Hashtable) requests.elementAt(i);
                request = new XmlRpcRequest((String) call.get("methodName"),
                                            (Vector) call.get("params"));
                Object handler = context.getHandlerMapping().getHandler(request.getMethodName());
                Vector v = new Vector();
                v.addElement(XmlRpcWorker.invokeHandler(handler, request, context));
                response.addElement(v);
            }
            catch (Exception x)
            {
                String message = x.toString();
                int code = (x instanceof XmlRpcException ?
                            ((XmlRpcException) x).code : 0);
                Hashtable h = new Hashtable();
                h.put("faultString", message);
                h.put("faultCode", new Integer(code));
                response.addElement(h);
            }
        }
        return response;
    }
}

Source


Since Magento support SOAP API why didn't you use SOAP API v1? because SOAP is powerful. try to go here What's the difference between XML-RPC and SOAP?

Parsing of Soap messages is not included in Android runtime, so it isn't really straightforward. You should use an external library. I'm using ksoap2.

If you search here on StackOverflow you'll see many examples on how to use it. For instance here

more references: link 1 link 2

MultiCall with PHP

$client = new Zend_XmlRpc_Client('http://magentohost/api/xmlrpc/');

// If somestuff requires api authentification,
// we should get session token
$session = $client->call('login', array('apiUser', 'apiKey'));

$client->call('call', array($session, 'somestuff.method', array('arg1', 'arg2', 'arg3')));
$client->call('call', array($session, 'somestuff.method', 'arg1'));
$client->call('call', array($session, 'somestuff.method'));
$client->call('multiCall', array($session,
     array(
        array('somestuff.method', 'arg1'),
        array('somestuff.method', array('arg1', 'arg2')),
        array('somestuff.method')
     )
));

// If you don't need the session anymore
$client->call('endSession', array($session));
Community
  • 1
  • 1
Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
  • 2
    First of all,I thank you for your efforts.Now, I would like to clarify things from your Answer.First, the MultiCall method that you have suggested is hardcoded for the client side which is totally wrong.We have a inbuilt Server Side multiCall() method in Magento,which is to be called from the Android client just like simple Call() method.You have understood the concept totally in a different way.Secondly,there are different XMLRPC libraries for Java and Android.The library that you have suggested here is of Java,not supported in Android.Still good try.Thanks. – Haresh Chaudhary Aug 20 '12 at 05:07
  • 2
    It's written that You want the multiCall function for android. I've edited my answer. – Josua Marcel C Aug 20 '12 at 05:18
  • Yes,see..you have updated for php,in which a method named multiCall(parameters) is called which already exist on the Server.Right.Similarly,I want to call that method from my Android Device. – Haresh Chaudhary Aug 20 '12 at 05:26
  • 2
    I would love to put the php code that you suggested right now in my Question so that it becomes my clearer for everyone.Thanks for improving my Question to Another Height. – Haresh Chaudhary Aug 20 '12 at 05:27
1

First login in whatever way works for calling catalog_product.list. Make sure session, client and product_ids have the right values. If you don't need to log in for these operations, set session = null (and if that doesn't work, try not passing session at all :) ). Then:

Object[][] calls = new Object[product_ids.length];
for (int i = 0; i < product_ids.length; i++) {
    calls[i] = new Object[] { "product_media.list", product_ids[i] };
}
product_media_ids = client.call("multiCall", new Object[] { session, calls });

product_media_ids should then be an array of arrays of product images - that is, each element of product_media_ids will be a return value from product_media.list.

The code is untested, I'm afraid.

Iain
  • 4,203
  • 23
  • 21
  • 1
    @laim :: I would test for you and reply with the Result. – Haresh Chaudhary Aug 20 '12 at 09:21
  • @Laim :: It gives Error like :: 08-02 09:03:36.931: WARN/System.err(24961): org.xmlrpc.android.XMLRPCFault: XMLRPC Fault: Calling parameters do not match signature [code 623] – Haresh Chaudhary Aug 20 '12 at 10:50
  • You need to find a way to work out if that error is thrown in the multiCall itself, or within a `product_media.list` call. Some approaches for doing that: make magento log really verbosely, or make `calls` an array of length 0, or replace the first entry in `calls` with something that has an observable effect on the server. – Iain Aug 20 '12 at 12:44
  • Did that error come from having `session` set correctly, set to null, or left out? – Iain Aug 20 '12 at 12:44
  • No,the error is thrown from the Server when the Method or Method parameters don'nt match that are in the Server Side.i have checked several other tactics like Confirming that the Session Id was correct,calling the Method without using the Session Id and likewise. – Haresh Chaudhary Aug 20 '12 at 12:55
  • Still,i personally feel that your solution must be nearer one..as it completely resembles that in the php..but have to work around it.Thanks from your precious time..I would not let your effort go in vain and will try in that way to get complete solution as Nothing on the Web have place an example for multiCall() for Android till date.Once again i reckon your Efforts. – Haresh Chaudhary Aug 20 '12 at 12:59
  • 2
    No worries. If I were you I'd be experimenting with 0-length multiCall bodies and with making magento print a ton of output (hacking at the source code if necessary, assuming you have a local dev instance you can do that to) to work out how far it's getting. Good luck! – Iain Aug 20 '12 at 14:06
  • On the basis Of the Closeness of the Answer,Iam awarding the bounty to you. – Haresh Chaudhary Aug 24 '12 at 09:42