xmlrpc
Below is an extremely simple xmlrpc client. This does not support multicalls or any other fancy functionality. This simply gives you the bare bones functionality.
I'm posting this here not as a question but simply as a reference and as a ploy to coax the Titanium devs to formally support the xmlrpc protocol.
/*
Simple xmlrpc client for sending requests and receiving results via a callback using standard Titanium http client mechanics.
Spec: http://www.xmlrpc.com/spec
*/
// string url: the full url to the server side xmlrpc script
// string method: the name of the xmlrpc method to call
// array params: list of strings to send
// func callback: callback function to call "onload"
function xmlrpc(url, method, params, callback)
{
Titanium.API.info("xmlrpc: begin");
Titanium.API.info("xmlrpc: url: " + url);
Titanium.API.info("xmlrpc: method: " + method);
var xhr = Titanium.Network.createHTTPClient();
xhr.open("POST",url);
xhr.onload = callback;
var xml = '<methodCall>';
xml += '<methodName>'+method+'</methodName>';
xml += '<params>';
for (var k in params)
{
if (k)
{
var p = params[k];
Titanium.API.info("xmlrpc: p: "+p);
xml += '<param><string>'+p+'</string></param>';
}
}
xml += '</params></methodCall>';
Titanium.API.info("xmlrpc: xml: "+xml);
xhr.send(xml);
Titanium.API.info("xmlrpc: end");
}
Save the above code into an xmlrpc.js file. Example use below:
var rpc_url = "http://example.com/xmlrpc.php";
function rpc_cb()
{
// you could use this.responseXML here too, this is just an example!
Titanium.API.info('Received: '+this.responseText);
}
var params = ['foobar'];
xmlrpc(rpc_url,"somesite.echo",params,rpc_cb);
8 Answers
-
Thanks, I have been wandering around in the darkness for the last two days, hoping to find some ray of light that would help me understand where the actual UI was located, I think I see that it is in the sandbox, anyway your example helped in ways I cannot begin to relate, thanks again.
-
@Joe Wheaton:
Be aware that if your xmlrpc.php script does not understand the "somesite.echo" rpc method then the callback will not be called. There is obviously no real error handling for this case. If I find the time I'll update this example with an error handling callback example. Don't know when I'll get to that though. No promises.
@Everyone else:
You're welcome!
-
I don't know how to thank you. This is seriously amazing! I got it working easily and it's saved me a LOT of headache! I just can't thank you enough. :D
A BIG HUGE THANKS!
-
I've tried this several times and I can't seem to get it to work. As is the callback function never gets called(possible bug). I modified it to use the receive method instead of send. It then calls the callback but there is nothing in the responseText, responseXML, or responseData.
Desktop Application
Titanium Developer 1.2.1 -
I got this working on the first try. Thanks for the help.
-
i am getting the following error as a XML Response
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>633</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Param must contain a value</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>can any one help me with this.
-
That example doesnt work for me neither. It never finds the method to call. Any working alternatives?
-
Hi there,
I came across this post in my search for a method to do xml-rpc calls. Thanks for your solution. I also made it more of a module to support all data types and parse the result. Hopefully someone finds it handy, https://gist.github.com/ceramedia/5626950