how to call a webService in titanium

You must Login before you can answer or comment on any questions.

I am new to titanium and and want to call a web service from my titanium app. The webService returns the json response. As I am aware of calling the webService using XMLRPC but very confused regarding json.

up-till now i know that we have to create the HTTPClient.

  var request = Titanium.Network.createHTTPClient();
  request.open("POST", "http://test.com/services/json");
  request.onload = function() {
  var content = JSON.parse(this.responseText);//in the content i have the response data

  };

 request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //did not understand this line
 request.send();

Now the problem is if my url(endpoints) have many WebServices, so where i will give the method name i.e WS name which is to be called.

From the API documentation of Titanium mobile the function open i.e. request.open accepts 3 parameters:

  1. method name (http method name)

  2. url of request

  3. async (boolean property) by default true.

In the above code what is "POST" doing there?? and if my WS name is system.connect then where i will be mentioning that in code?

And what if the WS needs parameter, so how can we send the parameter to the webService form the above code.

I know that request.send() can be used to send parameter but how ??

1 Answer

Hi Ajeet, The parameter in request.send() is the POST body. You can send parameters in form key1=value1&key2=value2 or json or what you want.

I good idea is to untrust datas from net and to code:

req.onload = function(_callback) {
    var res = this.responseText;
    try{ _callback(JSON.parse(res);)
    }
catch(E) {Ti.API.log(E);}
 
}

— answered 1 year ago by Rainer Schleevoigt
answer permalink
2 Comments
  • and where i will be defining the method name that i am going to call. for example i have a endpoint http://alpha.com/services/json now i have lot of WS so how will i call a specific webService. I have heard that we have to give method name somewhere.

    — commented 1 year ago by Ajeet pratap Maurya

  • Don't you have full URL through which your functionality will be called ? Just give that full URL. And you want to test your URL than just test in API console of apigee.com. It will helpfull to check whether your service is running or not.

    — commented 1 year ago by Yogin Bhungalia

Your Answer

Think you can help? Login to answer this question!