Back to top

Calling Drupal WebService through Titanium

In this blog I will brifly expain on calling Drupal webservice from Titanium application.

Calling a webService without any parameter

To call a DrupalWebservice , you will need the service end point. I am assuming reader already have some idea of creating and calling the webservice, here wewould be calling one of the default webservice using Titanium (http://www.appcelerator.com/)

We will use a default webservice which is system.connect. System.connect is used to generate php session id, which can then be used for making other webservice calls.

To call any webService we neet to create an httpClient in Titanimum. Below is the code you need for doing this.

 

var xhr = Titanium.Network.createHTTPClient({ onload : function(e) { //1 Titanium.API.info(this.responseText); }, onerror : function(e) { //2 Ti.API.debug(e.error); alert('error'); }, timeout : 5000 }); xhr.open("POST","http://myTestUrl.net/services/json"); //3 xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); //3 xhr.send("method=system.connect"); //4 }

 

 

 

 

Lets understand what above means:

  • onload : This Function to be called upon a successful response.In that function this.responseText will Log your response that you got from the server.
  • onerror : This Function to be called upon a error response.
  • xhr.open() : This Opens the request and readies the connection. here I am sending a POST request and my endpoint is http://myTestUrl.net/services/json
  • xhr.setRequestHeader() : Sets the value for the specified request header. This function must be called after open but before send.
  • xhr.send () : This method sends the request, as you can see the method name which is system.connect which I am passing.

So the above code will call the webService name "system.connect" and this webService does not accepts any parameter.
You can get your server response in onload() function. You can parse the response using "JSON.parse".

Calling webService with parameter

Now what if you want to send some parameter to the webService that you want to call. 
We will follow the same step as above but the only difference will be that we will pass the parameter with our send() request.
here how we do it:

 

var xhr = Titanium.Network.createHTTPClient({ onload : function(e) { Titanium.API.info(this.responseText); //do whatever you want to do with your response }, onerror : function(e) { alert('error'); }, timeout : 5000 }); xhr.open("POST","http://myTestUrl.net/services/json"); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); var param = { //1 "method" : "getData.list", "dataID" : "243" }; xhr.send(param);

TipIf the parameter JSON is more complex then while sending do

xhr.send(JSON.stringify(param))

Here you can see that the only difference you can see is in the send() function.
As now we dont have to send only our API name i.e method name, but we are passing an extra parameter here i.e "dataID".

NOTE: The names of your parameter should be same as you define in your webService API.

Hence In the above code we are calling a webService named "getData.list" which accepts one parameter "dataID".
we can send as many parameter by this way as required by our API.