Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var API_URL = 'https://www.googleapis.com/books/v1/volumes?q=';
- var MAX_BOOKS = 100;
- var SEARCH_TERM = "Harry Potter";
- var win = Ti.UI.createWindow({
- backgroundColor : "#FFF"
- });
- var section = Ti.UI.createListSection();
- var listView = Ti.UI.createListView({
- sections : [section]
- });
- win.add(listView);
- win.open();
- function processBookData(data) {
- var books = [];
- // make sure the returned data is valid
- try {
- var items = JSON.parse(data).items;
- } catch (e) {
- alert('Invalid response from server. Try again.');
- return;
- }
- // process each book, appending it to the list view
- for (var i = 0; i < Math.min(items.length, MAX_BOOKS); i++) {
- var info = items[i].volumeInfo;
- if (!info) {
- continue;
- }
- books.push({
- properties : {
- title : info.title
- }
- });
- }
- section.setItems(books);
- }
- function searchForBooks() {
- // validate search data
- var value = encodeURIComponent(SEARCH_TERM);
- // search Google Book API
- var xhr = Ti.Network.createHTTPClient({
- onload : function(e) {
- processBookData(this.responseText);
- },
- onerror : function(e) {
- alert('There was an error processing your search. Make sure you have a network connection and try again.');
- Ti.API.error('[ERROR] ' + (e.error || JSON.stringify(e)));
- },
- timeout : 5000
- });
- xhr.open("GET", API_URL + value);
- xhr.send();
- }
- searchForBooks();
Add Comment
Please, Sign In to add comment