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
[source language=”ruby”]
rhodes app rhodes-unit-webservice
rhodes model Unit value,from,to
[/source]
Enable sync in Model to connect to rhoconnect
[source language=”ruby”]
class Product
enable :sync
end
[/source]
Pass inputs given by user to rhoconnect search action
[source language=”ruby”]
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
[/source]
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
[source language=”ruby”]
rhoconnect app rhoconnect-unit-webservice
rhoconnect source Unit
[/source]
Add savon gem in Gemfile
[source language=”ruby”]
gem ‘savon’
[/source]
Call ChangeLengthUnit method in webservice using savon
[source language=”ruby”]
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
[/source]
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
[source language=”ruby”]
def search(params)
# params – values from rhodes
end
[/source]
We will assign the response of SOAP call to @result which will in turn stores in redis database as shown
[source language=”ruby”]
puts data.to_hash
@result = data.to_hash
@result
[/source]
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
[source language=”ruby”]
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
[/source]
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.
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..