Consume a Rest API and Persist the Data in a Local Database - Titanium

Asked

Viewed 1,084 times

0

I am using the Titanium framework to along with its Alloy plugin, and need to consume a REST api in my application, I would like to know a good way to consume this api and persist the JSON that it returns in a local database.

  • Have you checked this link? http://developer.appcelerator.com/question/145460/retrieve-json-from-server-and-commonjs-module

  • I had already implemented something similar, but this will help, thank you.

1 answer

0


Here is an example of how to make the requisitions, which was taken from: http://developer.appcelerator.com/question/145460/retrieve-json-from-server-and-commonjs-module

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;

Here’s an example of how to save the data in db local taken from Titanium documentation:

http://docs.appcelerator.com/platform/latest/#! /guide/Working_with_a_sqlite_database-Section-29004901_WorkingwithaSQLiteDatabase-Storingdata

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

Browser other questions tagged

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