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

December 26, 2011

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
          http = Net::HTTP.new('www.w3schools.com', 80)
          path = '/webservices/tempconvert.asmx' 
  2. Create the SOAP Request envelope
    data = <<-EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
      <CelsiusToFahrenheit xmlns="http://tempuri.org/">
            <Celsius>#{celsius}</Celsius>
      </CelsiusToFahrenheit>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    EOF
    
  3. Set Headers if required
    headers = {
            'Content-Type' => 'text/xml',
            'Host' => 'spritle.com'
          }
    
  4. Make the http request and then process the result data
    resp, data = http.post(path, data, headers)
    puts data
    
  5. Sample SOAP Response
    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CelsiusToFahrenheitResponse xmlns="http://tempuri.org/"><CelsiusToFahrenheitResult>89.6</CelsiusToFahrenheitResult></CelsiusToFahrenheitResponse></soap:Body></soap:Envelope>
    

If you want you can also the process the SOAP Response string using XML parsers like rhoxml.
Example:

xmldoc = REXML::Document.new(data)
xmldoc.elements.each("soap:Envelope/soap:Body/CelsiusToFahrenheitResponse/CelsiusToFahrenheitResult") { |e| puts e.text
}

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.

pixelstats trackingpixel

5 Comments. Leave new

Hi.

I’m trying to use savon with RhoMobile, can you share your code?

Thank you in advance,
jpsfs

Jose Marques
March 29, 2012 3:24 AM

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!

Balaji D Loganathan
April 6, 2012 12:16 PM

Sorry we dont have a sample for it. We opt for custom http post than going through the gem dependencies pain of savon.

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.

Vaibhav SHarma
October 18, 2012 3:34 PM

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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>