Titanium-HttpClient_send_xml_json_Tes

/app.js:

/ this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

// create tab group
var tabGroup = Titanium.UI.createTabGroup();


//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({ 
    title:'Tab 1',
    backgroundColor:'#fff',
    url:'json.js'
});
var tab1 = Titanium.UI.createTab({ 
    icon:'KS_nav_views.png',
    title:'Http Send JSON',
    window:win1
});


//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({ 
    title:'Tab 2',
    backgroundColor:'#fff',
    url:'xml.js'
});
var tab2 = Titanium.UI.createTab({ 
    icon:'KS_nav_ui.png',
    title:'Http Get XML',
    window:win2
});

 


//
//  add tabs
//
tabGroup.addTab(tab1); 
tabGroup.addTab(tab2); 


// open tab group
tabGroup.open();

json.js:

var win = Ti.UI.currentWindow;

var titleTxtLab = Titanium.UI.createLabel({
 text : 'UserName:',
 height : 'auto',
 width : 'auto',
 shadowColor : '#aaa',
 shadowOffset : {
  x : 5,
  y : 5
 },
 color : '#900',
 font : {
  fontSize : 20
 },
 textAlign : 'center',
 top : 10,
 left : 10
});
var titleTxt = Titanium.UI.createTextField({
 width : 150,
 value : 'UserName',
 borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
 top : 10,
 left : '40%'
});
win.add(titleTxtLab);
win.add(titleTxt);
var msgTxtLab = Titanium.UI.createLabel({
 text : 'Message:',
 height : 'auto',
 width : 'auto',
 shadowColor : '#aaa',
 shadowOffset : {
  x : 5,
  y : 5
 },
 color : '#900',
 font : {
  fontSize : 20
 },
 textAlign : 'center',
 top : 80,
 left : 10
});
var msgTxt = Titanium.UI.createTextArea({
 value : 'I am a textarea',
 height : 100,
 width : 150,
 font : {
  fontSize : 20,
  fontFamily : 'Marker Felt',
  fontWeight : 'bold'
 },
 color : '#888',
 textAlign : 'left',
 top : 80,
 left : '40%'
});

win.add(msgTxtLab);
win.add(msgTxt);
var sendJsonButton = Ti.UI.createButton({
 font : {
  fontSize : 20,
  fontWeight : 'bold'
 },
 title : 'sendMessage',
 touchEnabled : true,
 focusable : true,
 top : '60%',
 left : '40%'
});
win.add(sendJsonButton);
sendJsonButton.addEventListener('click', function(e) {
 var jsonSendData = {
  title : titleTxt.value,
  body : msgTxt.value
 };
 var url = "http://172.23.15.58:8080/WebService/GetJsonServlet";
 var xhr = Ti.Network.createHTTPClient({
  onload : function(e) {
   Ti.API.debug(this.responseText);
   var responseText = this.responseText;
   var jsonData = JSON.parse(this.responseText);
   alert(jsonData.username);
  },
  onerror : function(e) {
   // this function is called when an error occurs, including a timeout
   Ti.API.debug(e.error);
   // alert('error');
  },
  timeout : 5000  /* in milliseconds */
 });
 xhr.open("POST", url);
 xhr.send(jsonSendData);
});
win.open();

xml.js:

var win = Ti.UI.currentWindow;
var getXmlButton = Ti.UI.createButton({
 font : {
  fontSize : 20,
  fontWeight : 'bold'
 },
 title : 'get Xml Data',
 touchEnabled : true,
 focusable : true
});

win.add(getXmlButton);

getXmlButton.addEventListener('click', function(e) {
 var url = "http://172.23.15.58:8080/WebService/GetXmlServlet";
 var xhr = Ti.Network.createHTTPClient({
  onload : function(e) {
   var xml = this.responseXML.documentElement;
   var users = xml.getElementsByTagName("user")
   Ti.API.debug(users.length);
   var tableData = [];
   for(var i = 0; i < users.length; i++) {
    var row = Ti.UI.createTableViewRow();
    row.selectedBackgroundColor = '#fff';
    row.height = 70;
    // 畫像を配置する
    var imageView = Titanium.UI.createImageView({
     image : users.item(i).getElementsByTagName("userImage").item(0).text,
     width : 50,
     height : 50,
     top : 5,
     left : 10
    });
    row.add(imageView);

    // ラベルを配置する
    var username = Ti.UI.createLabel({
     color : '#576996',
     font : {
      fontSize : 16,
      fontWeight : 'bold',
      fontFamily : 'Arial'
     },
     left : 100,
     top : 2,
     height : 30,
     width : 100,
     text : users.item(i).getElementsByTagName("userName").item(0).text
    });
    row.add(username);
    // ラベルは省略
    var cellPhone = Ti.UI.createLabel({
     // (...)
     left : 100,
     top : 35,
     height : 30,
     width : 100,
     text : users.item(i).getElementsByTagName("cellPhone").item(0).text
    });
    row.add(cellPhone);
    // 以上の內容の行を追加する
    tableData.push(row);
   }
   var tableView = Titanium.UI.createTableView({
    data : tableData,
   });
   win.add(tableView);
   win.remove(getXmlButton);
  },
  onerror : function(e) {
   // this function is called when an error occurs, including a timeout
   Ti.API.error(e.error);
   // alert('error');
  },
  timeout : 5000  /* in milliseconds */
 });
 xhr.open("POST", url);
 xhr.send();
});
win.open();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章