2013-06-13 102 views
0
I have a json data in my remote server,and i want to show it in table view,here is my json data 

[當沒有什麼{「offerType」:「打折」,「offerName」:「巴寶莉 - 買2送1對男子的皮大衣集」}, { 「offerType」: 「打折」, 「offerName」: 「ARMANI JUNIOR- 20折優惠購買價值30000 INR」}]鈦:讓試圖從遠程服務器訪問JSON數據

 var url = "http://203.122.12.58:8080/api?    username=superuser&password=superuser&action=invokeService&serviceName=offerAction&methodNa me=getMyOffers&customerId=CUS-12220&storeId=CMPNY-3376"; 

var win = Ti.UI.createWindow(); 

win = Ti.UI.currentWindow; 
win.setBackgroundColor('gray'); 
//win.setBackgroundColor = 'white'; 
    var table = Ti.UI.createTableView(); 
    var tableData = []; 
    var json, fighters, fighter, i, row, nameLabel, nickLabel,offerType,offerName; 

var xhr = Ti.Network.createHTTPClient({ 
    onload: function() { 
// Ti.API.debug(this.responseText); 

json = JSON.parse(this.responseText); 
for (i = 0; i < json.length; i++) { 

    console.log("json = %d",i); 
    offerType = json[i].offerType; 
    row = Ti.UI.createTableViewRow({ 
     height:'60dp' 
    }); 
    nameLabel = Ti.UI.createLabel({ 
     text:offerType, 
     font:{ 
      fontSize:'24dp', 
     fontWeight:'bold' 
    }, 
    height:'auto', 
    left:'10dp', 
    top:'5dp', 
    color:'#000', 
    touchEnabled:false 
    }); 
    offerName = json[i].offerName; 
    nickLabel = Ti.UI.createLabel({ 
    text:'"' + fighter.nickname + '"', 
    font:{ 
     fontSize:'16dp' 
    }, 
    height:'auto', 
    left:'15dp', 
    bottom:'5dp', 
    color:'#000', 
    touchEnabled:false 
    }); 

    row.add(nameLabel); 
    row.add(nickLabel); 
    tableData.push(row); 
    } 

table.setData(tableData); 
}, 
onerror: function(e) { 
Ti.API.debug("STATUS: " + this.status); 
Ti.API.debug("TEXT: " + this.responseText); 
Ti.API.debug("ERROR: " + e.error); 
alert('There was an error retrieving the remote data. Try again.'); 
}, 
timeout:5000 
}); 

xhr.open("GET", url); 
xhr.send(); 

我如何能實現我的目標,在此先感謝。

Akshay

+0

哪裏錯誤?什麼是空的? JSON? json.length = 0?我認爲你用瀏覽器測試了Web服務響應?請提供更多的細節。 –

+0

嗨Tevo,我解決了我的問題,實際上有問題與模擬器,當我重新啓動我的模擬器它的作品我的expectation.Thanks您的迴應 – Akshay

+0

但現在我想添加一個圖像到我的窗口,我想我寫正確的代碼,但我得到的錯誤資源/ main_folder/imgshop.png:沒有找到路徑,你可以在this.thanks阿克沙伊 – Akshay

回答

1

您的代碼非常接近。我做了一些更改,因此您必須對其進行比較以瞭解如何解決問題。本示例讀取您的服務並在表格中顯示2個條目。

看到代碼的細節它是如何改變的意見。

//**** Not sure if this is an issue with formating on Stackoverflow or copy and paste, but the spaces in the URL are causing an issue.**** 
//var url = "http://203.122.12.58:8080/api?    username=superuser&password=superuser&action=invokeService&serviceName=offerAction&methodNa me=getMyOffers&customerId=CUS-12220&storeId=CMPNY-3376"; 
var url = "http://203.122.12.58:8080/api?username=superuser&password=superuser&action=invokeService&serviceName=offerAction&methodName=getMyOffers&customerId=CUS-12220&storeId=CMPNY-3376"; 

var win = Ti.UI.createWindow(); 
//*****If you are calling this from another window, you may need this is your overall code, but my example I removed it.***** 
//win = Ti.UI.currentWindow; 
win.setBackgroundColor('gray'); 
//win.setBackgroundColor = 'white'; 
var table = Ti.UI.createTableView(); 
var tableData = []; 
// **** Removed variable fighters because it is never used. 
// **** Removed the fighter variable because it is never used. 
var json, i, row, nameLabel, nickLabel, offerType, offerName; 

var xhr = Ti.Network.createHTTPClient({ 
    onload: function() { 
     // Ti.API.debug(this.responseText); 
     json = JSON.parse(this.responseText); 
     for (i = 0; i < json.length; i++) { 
      console.log("json = %d",i); 
      offerType = json[i].offerType; 
      row = Ti.UI.createTableViewRow({ 
       height:'60dp' 
      }); 
      nameLabel = Ti.UI.createLabel({ 
       text: offerType, 
       font:{ 
        fontSize:'24dp', 
        fontWeight:'bold' 
       }, 
       height:'auto', 
       left:'10dp', 
       top:'5dp', 
       color:'#000', 
       touchEnabled:false 
      }); 
      offerName = json[i].offerName; 
      nickLabel = Ti.UI.createLabel({ 
      //text:'"' + fighter.nickname + '"', ***** fighter.nickname isn't defined anywhere so it is giving an error. You defined offerName right above it so I assume that it 
      // was the intended variable here. 
       text: offerName, 
       font:{ 
        fontSize:'16dp' 
       }, 
       height:'auto', 
       left:'15dp', 
       bottom:'5dp', 
       color:'#000', 
       touchEnabled:false 
      }); 

      row.add(nameLabel); 
      row.add(nickLabel); 
      tableData.push(row); 
     } 

     table.setData(tableData); 
    }, 
    onerror: function(e) { 
     Ti.API.debug("STATUS: " + this.status); 
     Ti.API.debug("TEXT: " + this.responseText); 
     Ti.API.debug("ERROR: " + e.error); 
     alert('There was an error retrieving the remote data. Try again.'); 
    }, 
    timeout:5000 
}); 

xhr.open("GET", url); 
xhr.send(); 
// Thought this might just be a test application, the table was never added to the window, so it would never be displayed so we can see the data. This is how you add the table. 
win.add(table); 

// I added code here to OPEN the window so I could see something displayed. Otherwise I just get the Appcelerator splash screen 
win.open(); 
+0

這不是其他人的一個非常有用的答案。請嘗試解釋你改變了什麼,以及用戶做錯了什麼! –

+0

這是一個很好的觀點。我在整個代碼中放置了評論以顯示前後的內容。這應該能夠更容易地看到問題以及需要修改哪些內容來解決問題。 – Martin