REST Connection
Hello!
I want to fill my Table with data of a RESTful webservice.
e.g. i got www.example.com/category/games as URL and it shows me my data.
Is there a way to put them into the table?
I'm very new to such topics but need to know hot thats possible.
Thanks!
3 Answers
-
Hi Michael, that's a fairly big topic, but try this for example:
//First, you'll want to check the user can access the web: if (Titanium.Network.online ==true) { //Then you'll need to create an object for your request and open it with the type of call and URL: var request = Titanium.Network.createHTTPClient(); request.open('GET'',www.example.com/category/games'); //And then you send it with: request.send(); //Next specify a function to run when the response comes back (using a .onload event): request.onload = function requestReceived() { // now the response status will tell you if it's worked (or the response is actually an error var statusCode = request.status; if (statusCode == 200) // 200 means it's OK, so as I'm getting XML data I extract the response using .responseXML { var response = request.responseXML; // you now have an object containing your data, so will need to parse it into an array, to go into the tableView tableviewArray = []; var itemList = response.documentElement.getElementsByTagName('item'); for (var c=0;c < itemList.length;c++) { var foo = ""; var foo = itemList.item(c).getElementsByTagName("element").item(0).text; tableviewArray.push({Title:foo}); }; //finally, create the tableview using the array just populated and add it to the current window: var myTableview = Titanium.UI.createTableView({data:tableviewArray}); Titanium.UI.currentWindow;.add(myTableview); }; }; };
And if all that works… I think you owe me a beer!
cheers,
Chris. -
Hi Chris,
this is an really good example, but i think i got an error in my implementation.
For example, the XML i get from www.example.com/category/games
looks like this:
-<games> <game>1345</game> <game>1253</game> <game>1238</game>
I set in your snippet "item" and "element" as "games" and "game".
// you now have an object containing your data, so will need to parse it into an array, to go into the tableView tableviewArray = []; var itemList = response.documentElement.getElementsByTagName('game'); for (var c=0;c < itemList.length;c++) { var foo = ""; var foo = itemList.item(c).getElementsByTagName("games").item(0).text; tableviewArray.push({Title:foo}); };
I get till now only a blank table.
You'll get your beer, thats for sure ;)
But it would be nice if you (or anyone else) have some hints for this.
Greetings,
Michael -
I have not tried the other methods (though I think I will)
But they use an HttpClient as Opposed to Backbone (js)
see here.. https://github.com/viezel/napp.alloy.adapter.restapi
Its really nice because it handles all table operations for you (if there is an update to the data set you dont have to update anything else)
I have got it working but having trouble customizing it. But regardless, its another option