Download Json com for Titanium

Asked

Viewed 27 times

1

I’m having trouble downloading the json with arrays, it is only inserting the last result, I do not know how it will insert every loop someone can help me follow the code:

My backend:

var xhr = Ti.Network.createHTTPClient();
var signos;
//var dataBase = require('dataBase');   
//dataBase('create', null, null, null);
//var signos = ['aries','taurus','gemini','cancer','leo','virgo','libra','scorpio','sagitarius','capricorn','aquarius','pisces'];
var countt = 0;
exports.receberSignos = function(signo, contador) {
    signos = signo;
    countt = contador;
};
exports.getHoroscopo = function(callback) {

    var url = 'http://theastrologer-api.herokuapp.com/api/horoscope/' + signos + '/today';
    xhr.open('GET', url);
    alert(countt);

    xhr.onload = function() {
        var data = JSON.parse(this.responseText);
        callback(data);
    };
    Ti.API.info('ss ' + countt + ' signos ' + signos);
    xhr.onerror = function(e) {
        alert(e.error);
    };
    xhr.send();
};

The class that calls the backend and inserts:

signos.forEach(function(value, index) {
    Ti.API.info('index  ' + index + ' value ' + value);
    if (index < 12) {
        backend.receberSignos(value, index);
        backend.getHoroscopo(function(sunsign) {
            Ti.API.info('signo2 ' + sunsign.sunsign + ' cont ' + cont);
            dataBase('insert', sunsign.sunsign, sunsign.date, sunsign.horoscope, a.toString());
            a++;
            if (cont == true) {
                carregar();
            }
        });
    }
});
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.