Consume an API Rest and Persist the data in a local bank - Titanium

0

I'm using the Titanium framework together with your Alloy plugin, and I need to consume a REST api in my application, I'd like to know a good way to consume this api and persist the JSON that it returns to a local bank. >     

asked by anonymous 28.07.2015 / 17:46

1 answer

0

Here is an example of how to make the requisitions, which was taken from:   link

function stringify(obj) {
    var arr = [], itm;
    for (itm in obj) {
        arr.push(itm + "=" + escape(obj[itm]));
    }
    return arr.join("&");
}
function download(obj) {
    var xhr = Ti.Network.createHTTPClient();
    var strMode = (obj.method || 'GET');
    xhr.setTimeout(obj.timeout || 10 * 1000);
    xhr.onload = function (e) {
        var strType = (obj.type.toLowerCase() || 'json');
        switch (xhr.status) {
        case 200:
            response = this.responseText;
            switch (strType) {
            case 'json':
                json = JSON.parse(response);
                if (obj.success) {
                    obj.success(json);
                }
                break;
            case 'html':
                if (obj.success) {
                    obj.success(response);
                }
                break;
            };
            break;
        case 304:
            if (obj.success) {
                obj.success([]);
            }
            break;
        case 404:
            if (obj.error) {
                obj.error({
                    responseText: 'Page Not Found',
                    status: xhr.status
                });
            }
            break;
        }
    };
    if (obj.error) {
        xhr.onerror = function (e) {
            obj.error(e);
        };
    }
    if (obj.progress) {
        xhr.onsendstream = function (e) {
            if (typeof(obj.progress) !== 'undefined') {
                obj.progress({
                    value: parseFloat((e.progress * 100), 10)
                });
            }
        };
    }
    if (obj.state) {
        xhr.onreadystatechange = function (e) {
            var state = this.readyState;
            var states = [
                'Unsent',
                'Opened',
                'Headers',
                'Loading',
                'Done'
            ];
            obj.state({
                state: state,
                caption: states[state]
            });
        };
    }

    if (strMode === 'POST') {
        xhr.open(strMode, obj.url);
        xhr.send(obj.param);
    } else {
        xhr.open(strMode, obj.url + '?' + stringify(obj.param));
        xhr.send();
    }
}
exports.download = download;

And here's an example of how to save the data in local db taken from the titanium documentation:

link

db.execute('INSERT INTO city (name,continent,temp_f,temp_c,condition_id) VALUES (?,?,?,?,?)', importName, importContinent, importTempF, importTempC, dbConditionId);
var lastID = db.lastInsertRowID; // presumes 'city' has an auto-increment column
    
29.07.2015 / 20:42