Titanium Mobile/Web APIÏ¢·È/Web¥µ¡¼¥Ó¥¹¤ÈÏ¢·È¤·¤¿¥¢¥×¥êºîÀ®¹ÖºÂ
¥¹¥Þ¡¼¥È¥Õ¥©¥ó¥¢¥×¥ê³«È¯´Ä¶¡ÖTitanium¡×¤òÃΤë¹ÖºÂ - Web¥µ¡¼¥Ó¥¹¤ÈÏ¢·È¤·¤¿¥¢¥×¥êºîÀ®¤Ë¹Ô¤Ã¤Æ¤¤Þ¤·¤¿¡£ HTTP¤ò»ÈÍѤ·¤¿¥½¡¼¥¹¤¬¿§¡¹»²¹Í¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
(1) ¥Ö¥é¥¦¥¶¤Ç»î¤·¤ËQiita¤ÎAPI¤Ë¥¢¥¯¥»¥¹
(2) ¥×¥í¥¸¥§¥¯¥È¤òºîÀ®
- [File] - [New] - [Mobile Project] - [Classic] - [Default Project]
- Project name: QiitaViewer
- App Id: jp.papuu.QiitaViewer
- iPhone, Android¤Î¤ß¤Ë¥Á¥§¥Ã¥¯
(3) app.js¤ò¿§¡¹Á´½ñ¤´¹¤¨¤·¤Æ»î¤¹
(a) httpClient¤òÍѤ¤¤¿´ðËÜŪ¤Ê¥×¥í¥°¥é¥à
#pre{{
var xhr, qiitaURL, method;
qiitaURL = 'http://qiita.com/api/v1/items';
method = 'GET';
xhr = Ti.Network.createHTTPClient();
xhr.open(method, qiitaURL);
xhr.onload = function() {
var body;
if (this.status === 200) {
body = JSON.parse(this.responseText);
Ti.API.info(body);
} else {
Ti.API.info('error: status code is ' + this.status);
}
}
xhr.onerror = function(e) {
var error;
error = JSON.parse(this.responseText);
Ti.API.info(error.error);
}
xhr.timeout = 5000;
xhr.send();
}}
(b) ¥¢¥×¥êÃæ¤Îsample.json¤òTableView¤Çɽ¼¨
¤Þ¤º¤ÏÄÌ¿®Ìµ¤·¤Ç¡¢¥¢¥×¥êÆâ¤ËÇÛÃÖ¤·¤¿json¥Õ¥¡¥¤¥ë¤ÎÆâÍÆ¤òTableView¤Çɽ¼¨¤·¤Æ¤ß¤Þ¤¹¡£
json¥Õ¥¡¥¤¥ë¤Ï¼¡¤òResources¥Õ¥©¥ë¥Àľ²¼¤ËÇÛÃÖ¤·¤Þ¤¹¡£
https://raw.github.com/h5y1m141/20130817-tistudy/master/sample.json
#pre{{
var sample, file, body, mainTable, win, i, len, row, rows, textLabel;
sample = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'sample.json');
file = sample.read().toString();
body = JSON.parse(file);
win = Ti.UI.createWindow({
title: 'QiitaViewer'
});
mainTable = Ti.UI.createTableView({
width: 'auto',
height: 'auto',
backgroundColor: '#fff',
left: 0,
top: 0
});
rows = [];
for (i = 0, len = body.length; i < len; i++) {
row = Ti.UI.createTableViewRow({
width: 'auto',
height: 40,
borderWidth :0,
className: 'entry',
color: '#222'
});
textLabel = Ti.UI.createLabel({
width: 'auto',
height: 30,
top: 5,
left: 5,
color: '#222',
cont: {
fontSize: 16,
fontWeight: 'bold'
},
text: body[i].title
});
row.add(textLabel);
rows.push(row);
}
mainTable.setData(rows);
win.add(mainTable);
win.open();
}}
(c) QiitaÅê¹Æ¾ðÊó¤Î¥Æ¥¥¹¥È¤òTableView¤Çɽ¼¨
#pre{{
var xhr, qiitaURL, method, mainTable, win;
win = Ti.UI.createWindow({
title: 'QiitaViewer'
});
mainTable = Ti.UI.createTableView({
width: 'auto',
height: 'auto',
backgroundColor: '#fff',
left: 0,
top: 0
});
qiitaURL = 'http://qiita.com/api/v1/items';
method = 'GET';
xhr = Ti.Network.createHTTPClient();
xhr.open(method, qiitaURL);
xhr.onload = function() {
var body, i, len, row, rows, textLabel, iconImage, imagePath;
if (this.status === 200) {
body = JSON.parse(this.responseText);
rows = [];
for (i = 0, len = body.length; i < len; i++) {
Ti.API.info(body[i].title);
row = Ti.UI.createTableViewRow({
width: 'auto',
height: 40,
borderWindth: 0,
className: 'entry',
color: '#222'
});
textLabel = Ti.UI.createLabel({
width: 'auto',
height: 30,
top: 5,
left: 5,
color: '#222',
font: {
fontSize: 16,
fontWeight: 'bold'
},
text: body[i].title
});
row.add(textLabel);
rows.push(row);
}
mainTable.setData(rows);
win.add(mainTable);
win.open();
} else {
Ti.API.info('error: status code is ' + this.status);
}
};
xhr.onerror = function(e) {
var error;
error = JSON.parse(this.responseText);
Ti.API.info(error.error);
}
xhr.timeout = 5000;
xhr.send();
}}
(d) QiitaÅê¹Æ¾ðÊó¤Î¥¿¥¤¥È¥ë¡¦¥¢¥¤¥³¥ó¤òTableView¤Çɽ¼¨
#pre{{
var xhr, qiitaURL, method, mainTable, win;
win = Ti.UI.createWindow({
title: 'QiitaViewer'
});
mainTable = Ti.UI.createTableView({
width: 'auto',
height: 'auto',
backgroundColor: '#fff',
left: 0,
top: 0
});
qiitaURL = 'http://qiita.com/api/v1/items';
method = 'GET';
xhr = Ti.Network.createHTTPClient();
xhr.open(method, qiitaURL);
xhr.onload = function() {
var body, i, len, row, rows, textLabel, iconImage, imagePath;
if (this.status === 200) {
body = JSON.parse(this.responseText);
rows = [];
for (i = 0, len = body.length; i < len; i++) {
row = Ti.UI.createTableViewRow({
width: 'auto',
height: 60,
borderWindth: 0,
className: 'entry',
color: '#222'
});
textLabel = Ti.UI.createLabel({
width: 250,
height: 30,
top: 5,
left: 60,
color: '#222',
font: {
fontSize: 16,
fontWeight: 'bold'
},
text: body[i].title
});
imagePath = body[i].user.profile_image_url;
iconImage = Ti.UI.createImageView({
width: 40,
height: 40,
top: 5,
left: 5,
defaultImage: 'logo.png',
image: imagePath
});
row.add(textLabel);
row.add(iconImage);
rows.push(row);
}
mainTable.setData(rows);
win.add(mainTable);
win.open();
} else {
Ti.API.info('error: status code is ' + this.status);
}
};
xhr.onerror = function(e) {
var error;
error = JSON.parse(this.responseText);
Ti.API.info(error.error);
}
xhr.timeout = 5000;
xhr.send();
}}