Consume SOAP webservice in rhoconnect

Rhomobile is cross-platform mobile framework to develop native application. The documentation for developing native app is nice. I recently tried consuming webservice using savon gem in rhoconnect. The most tricky part is to get input from rhodes application and use that as a parameters in rhoconnect webservice call. The gist of things needed for making SOAP call are

    * Create a rhodes application
    * Pass the input parameters to rhoconnect application
    * Make a SOAP request in rhoconnect
    * Display the SOAP response in rhodes from rhoconnect

We will use http://www.webservicex.net/length.asmx?WSDL webservice to consume and manipulate response in rhoconnect. Let’s see how to make these things work.

1. Sample Rhodes Application with search action

    First we will create a rhodes app with model Unit as shown

    	rhodes app rhodes-unit-webservice
    	rhodes model Unit value,from,to
    	

    Enable sync in Model to connect to rhoconnect

    	class Product
    	enable :sync
    	end
    	

    Pass inputs given by user to rhoconnect search action

    	def search
             Unit.search(
                :from => 'search',
                :search_params => {
                    :Value => @params['unit']['value'],
                    :From => @params['unit']['from'],
                    :To => @params['unit']['to']
                     },
                :callback => url_for(:action => :search_callback),
                :callback_param => ""
            )
           render :action => :wait
          end
          

    We will implement search callback after SOAP request done in rhoconnect. Now let’s see what to do in rhoconenct

2. Rhoconnect Application with SOAP webservice call in search action

    We will create a basic rhoconnect application with Unit source adapter as shown

    	  rhoconnect app rhoconnect-unit-webservice
    	  rhoconnect source Unit
    	  

    Add savon gem in Gemfile

    	  gem 'savon'
    	  

    Call ChangeLengthUnit method in webservice using savon

    	  require 'savon'
              @webservice_url = "http://www.webservicex.net/length.asmx?WSDL"
              client = Savon::Client.new(@webservice_url )
              data= client.request :change_length_unit do
              soap.body = {
               "LengthValue" => params["Value"],
               "fromLengthUnit" => params["From"],
               "toLengthUnit" => params["To"]
                }
              end
              puts data.to_hash
    	  

    Here data.to_hash has the response of webservice called. We will get inputs for this SOAP request from rhodes search action. We can access those params in rhoconnect search action as

              def search(params)
    	  # params - values from rhodes
    	  end
    	  

    We will assign the response of SOAP call to @result which will in turn stores in redis database as shown

    	  puts data.to_hash
    	  @result = data.to_hash
    	  @result
              

3. Display SOAP response from rhoconnect

    Now response of SOAP request is stored in redis database. We can access it in search callback in rhodes as

    	def search_callback
              status = @params["status"]
              if status == "complete"
                 data = Unit.find_all
                 data.each do |val|
                   Alert.show_popup("The Converted value is #{val.change_length_unit_result}")
                 end
              WebView.navigate(url_for :action => :index)
             end
           end
           

    All values stored in rhoconnect can be retrieved through find query. This is how we consume SOAP webservice from rhodes through rhoconnect.

Some of the screenshots taken from Iphone Mobile

I hope this blog will be helpful for making SOAP request in rhomobile. Source code of rhodes and rhoconnect application is available at https://github.com/Dhepthi/rhodes-unit-webservice, https://github.com/Dhepthi/rhoconnect-unit-webservice.Please feel free to improve and send me a pull request. Any suggestion or comments are welcome.

pixelstats trackingpixel

2 Comments. Leave new

Error hitting like this…can you mail how to run this code

APP| [Debugger] Unable to open connection to debugger: #

APP| Params: {“sync_type”=>”incremental”, “status”=>”error”, “error_code”=>”7″, “error_message”=>””, “rho_callback”=>”1″, “search_params”=>{“Value”=>”6″, “From”=>”Feet”, “To”=>”Inches”}}

Error hitting on me…can you mail how to run this code..

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>