【问题标题】:Android-Magento- How to Get the Details of Multiple Products in Android using XML-RPCAndroid-Magento - 如何使用 XML-RPC 获取 Android 中多个产品的详细信息
【发布时间】:2012-08-06 09:46:15
【问题描述】:

如何使用 Magento 的 XMLRPC 在 Android 中的 Single Call 中获取多个产品的详细信息。我能够使用函数 catalog_product.list 获取产品列表strong> 使用 XMLRPC。

现在,我有了所有产品的 SKU id。我可以使用函数 product_media.list 获取每个产品的媒体详细信息。

如果假设我有 10 个产品,我必须为每个产品调用 10 次 product_media.list 方法,这需要很长时间。

那么,如何在Android中调用MagentomultiCall函数。 php 中发布了许多调用 multiCall 函数的教程,但我无法在 Android 中模仿。

如果你有类似的代码 sn-p 可以让我理解 multiCall 函数(for Android),请帮助我,以便我可以进一步使用它.
谢谢。


Josua Marcel C 的回答中的 PHP 代码示例


$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')
                   )
              )
            );  

我想在Android中模仿上面的php代码调用Magento的multiCall()函数。


【问题讨论】:

  • 是否有您用来调用 catalog_product.list 和 product_media.list 的 XML-RPC 库?您能谈谈您已经尝试过使用 multiCall 吗?
  • @lain 是的,我正在使用 XML-RPC 库来调用每个列表和其他数据。我自己尝试了许多生动的方法,但每次都失败了。最糟糕的是,没有一个例子我在 10 天的研究中发现,可以提示我在 Android 中使用 multiCall() 方法。
  • 如果我只使用 call() 方法,那么我可以获取所有内容。但您可能知道,即使 5 次调用使用 call 方法也需要更多时间。而且 multiCall 的主要优势() 方法是它会在一次调用中接受“客户端的所有调用”并给我带来一个结果。
  • 您使用的是哪个 XML-RPC 库?
  • @laim 是安卓版的--code.google.com/p/android-xmlrpc

标签: java android magento xml-rpc


【解决方案1】:

答案

由于android是基于java的应用,你可以用这个。

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


既然 Magento 支持 SOAP API,为什么不使用 SOAP API v1?因为 SOAP 很强大。试试看这里What's the difference between XML-RPC and SOAP?

Soap 消息的解析不包含在 Android 运行时中,因此并不是很简单。您应该使用外部库。我正在使用 ksoap2。

如果您在这里搜索StackOverflow,您会看到许多有关如何使用它的示例。比如here

更多参考:link 1link 2

使用 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));

【讨论】:

  • 首先,感谢您的努力。现在,我想从您的回答中澄清一些事情。首先,您建议的 MultiCall 方法是为客户端硬编码的,这是完全错误的.我们在 Magento 中有一个内置的服务器端 multiCall() 方法,它可以像简单的 Call() 方法一样从 Android 客户端调用。你对这个概念的理解完全不同。其次,有不同的 XMLRPC 库用于Java 和 Android。您在此处建议的库是 Java 的,Android 不支持。还是不错的尝试。谢谢。
  • 上面写着你想要android的multiCall功能。我已经编辑了我的答案。
  • 是的,请参阅..您已经更新了 php,其中调用了一个名为 multiCall(parameters) 的方法,该方法已经存在于 Server.Right.Similarly,我想从我的 Android 调用该方法设备。
  • 我很乐意将您现在建议的 php 代码放在我的问题中,以便每个人都更清楚。感谢您将我的问题改进到另一个高度。
【解决方案2】:

经过长期研究,我得到中途解决方案调用 multiCall() 方法没有任何Error,但我仍然不知道如何在变量中获取Server 的响应并使用它。

任何有知识的人都可以编辑我的Answer,我会感谢他的。

我用过的Code是:

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();
}  

致谢:

我已经处理了 Iain 发布的答案。

【讨论】:

    【解决方案3】:

    以任何适合调用catalog_product.list 的方式首次登录。确保 sessionclientproduct_ids 具有正确的值。如果您不需要登录这些操作,请设置session = null(如果这不起作用,请尝试根本不通过会话:))。那么:

    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 应该是一个产品图像数组的数组——也就是说,product_media_ids 的每个元素都是来自product_media.list 的返回值。

    恐怕代码未经测试。

    【讨论】:

    • @laim :: 我会为你测试并回复结果。
    • @Laim :: 它给出的错误类似于 :: 08-02 09:03:36.931: WARN/System.err(24961): org.xmlrpc.android.XMLRPCFault: XMLRPC Fault: Calling parameters do不匹配签名 [代码 623]
    • 您需要找到一种方法来解决该错误是在 multiCall 本身中还是在 product_media.list 调用中引发的。一些方法可以做到这一点:使 magento 日志非常冗长,或使 calls 成为长度为 0 的数组,或将 calls 中的第一个条目替换为对服务器有明显影响的内容。
    • 该错误是由于正确设置了session、设置为空还是忽略了?
    • 不用担心。如果我是你,我会尝试使用 0 长度的 multiCall 主体,并让 magento 打印大量输出(如果有必要,在源代码上进行黑客攻击,假设你有一个本地开发实例,你可以这样做)来解决如何它已经走了很远。祝你好运!
    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 2012-05-28
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多