Consume SOAP Service from RhoMobile (iPhone, Android, WM and so on) apps

Balaji D Loganathan

50 sec read

We wanted to consume simple SOAP based web services from the iPhone, Android applications using RhoMobile framework. We dont want to use Savon as it got too much dependency to become rhodes-ruby extension.
Just thought lets write the raw soap client without using SOAP gems. Rhodes already support net-http and rexml(rhoxml) for such tasks.

The sample source code can be found at https://github.com/spritle/rhodes_soap_test/blob/master/app/Soap/soap_controller.rb which basically consume the SOAP services from http://www.w3schools.com/webservices/tempconvert.asmx
The basic steps involve on the controller side involves:

  1. Initialize the http object with soap service url
    [source language=”ruby”]
    http = Net::HTTP.new(‘www.w3schools.com’, 80)
    path = ‘/webservices/tempconvert.asmx’ [/source]
  2. Create the SOAP Request envelope
    [source language=”ruby”]
    data = <<-EOF



    #{celsius}



    EOF
    [/source]
  3. Set Headers if required
    [source language=”ruby”]
    headers = {
    ‘Content-Type’ => ‘text/xml’,
    ‘Host’ => ‘spritle.com’
    }
    [/source]
  4. Make the http request and then process the result data
    [source language=”ruby”]
    resp, data = http.post(path, data, headers)
    puts data
    [/source]
  5. Sample SOAP Response
    [source language=”ruby”]
    89.6
    [/source]

If you want you can also the process the SOAP Response string using XML parsers like rhoxml.
Example:
[source language=”ruby”]
xmldoc = REXML::Document.new(data)
xmldoc.elements.each(“soap:Envelope/soap:Body/CelsiusToFahrenheitResponse/CelsiusToFahrenheitResult”) { |e| puts e.text
}
[/source]
If you want to see how we can use savon as rhodes-ruby-extensions, please leave us a comment so that we can share our code on Github.

Related posts:

5 Replies to “Consume SOAP Service from RhoMobile (iPhone, Android, WM and…”

  1. I am trying to use savon with rhomobile, and getting into a lot of trouble with its dependencies….
    If you got a sample running, can you post it?
    Thanks!

  2. I am trying to consume a soap webservice in a rhomobile app, so this blog looked perfect.
    However I have added to the extensions in the build file but I must be missing something (very new to rhodes/ruby)
    When I try to create the http object on 1. I get an error.
    I’m getting ‘source not found’ within a tab labelled ‘extensions/net-http/net/http’
    Any help appreciated – thanks.

  3. Hi,
    I am trying to consume SOAP service likewise and it is working good in rho simulator but when I bulid it on wondows mobile CE 6.5, application closes as soon as I click on my button to call that action, I have used my SOAP service almost the same way as it shown above
    please help

Leave a Reply

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