[AC-1979] Problems, retrieve data WebService HTTPClient within callback function, asynchronous
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | n/a |
| Status | Closed |
| Resolution | Cannot Reproduce |
| Resolution Date | 2013-03-14T16:47:03.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | n/a |
| Components | Appcelerator Modules, Documentation, Studio, Titanium SDK & CLI |
| Labels | android |
| Reporter | diego |
| Assignee | Shak Hossain |
| Created | 2013-03-11T02:33:42.000+0000 |
| Updated | 2016-03-08T07:40:55.000+0000 |
Description
please, I have problems using the callback function when I'm waiting data from a webservice, I need to split the code to stay in separate classes or applying a standard structure commonjs, this is my code but i cant catch whats wrong, i cant see my alerts messages.
File: MisWebServices.js
exports.consumirWS = function(callback) {
var client = Ti.Network.createHTTPClient({
onload : function(e) {
if (callback) {
callback(this.responseText);//I also did the test here with this.responseXML
}
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout : 5000 // in milliseconds
});
client.open("GET", "http://www.miurl.com");
client.send();
}
Now, my file app.js
WS = require('/MisWebServices');
function callback(datos){
try {
alert('b'+JSON.stringify(datos)); // Displays information in text format
//If I use the JSON.parse also not work
//var datosJSON = JSON.parse(datos);
var items = datos.documentElement.getElementsByTagName("element");
var arrData = [];
for (var i=0; i < items.length; i++) {
arrData.push({
Country: items.item(i).getElementsByTagName("Country").item(0).text,
Location: items.item(i).getElementsByTagName("Location").item(0).text
});
}
for (var i = 0; i < arrData.length; ++i) {
//now here, i cant see de alert messages :(
alert('country:'+arrData[i].Country);
alert('state:' +arrData[i].Location);
}
} catch(erro) {
var items = {error : 'Invalid JSON data'};
}
}
//Call the service as follows
WS.consumirWS(callback);
I cannot reproduce the problem; I tried the code you provided, with the same URL, and I get an error (in fact, the page gives back a 404). If I use a valid URL, everything works fine, and I see the result of the call. Thanks
I tested this issue the test code below. I can’t reproduce this issue in latest Ti SDK. It’s working perfect as we expected. Please test this code in latest SDK and let me know if you have any issue. I hope it solve your problem.
Test Environment
MAC OS X 10.8.5, Titanium SDK 3.2.0 GA IOS Simulator 7.0.3 Android 2.3.3 Ti CLI 3.2.0Test Code
Thanksvar win = Titanium.UI.createWindow({ backgroundColor : '#fff' }); // Create table var table = Ti.UI.createTableView({ }); win.add(table); // Create http client c = Titanium.Network.createHTTPClient(); c.setTimeout(10000); c.onload = function() { var xml = this.responseXML; // the blog's title is in a node named "channel" //var channel = xml.documentElement.getElementsByTagName("channel"); // begin looping through blog posts var data = []; // blog posts are in nodes named "item" var items = xml.documentElement.getElementsByTagName("item"); for (var i=0;i<items.length;i++) { data.push({ postTitle: items.item(i).getElementsByTagName("title").item(0).text, postLink: items.item(i).getElementsByTagName("link").item(0).text }); } Ti.API.info('IN ONLOAD '); for (var i = 0; i <= data.length; i++) { alert('Title:'+data[i].postTitle); alert('link:' +data[i].postLink); }; } c.onerror = function(e) { Ti.API.info('XHR Error ' + e.error); }; // open the client c.open('GET', 'http://apod.nasa.gov/apod.rss'); // send the data c.send(); win.open();