2013-06-13 6 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 

[JSON 데이터에 액세스하려고 할 때의 어떠한 얻기 없습니다 - {: "할인", "offerName" "offerType"을 "버버리 (2) 남성 가죽 코트 컬렉션 1 무료로 얻을 구매"} { "offerType": "할인", "offerName": "구매 가치가 INR 30000에 ARMANI JUNIOR- 20 % 할인"}] 난 내 목표를 달성 할 수있는 방법티타늄 : 원격 서버에서

 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.length = 0? 웹 서비스 응답을 브라우저로 테스트했다고 가정합니까? 자세한 내용을 입력하십시오. –

+0

안녕하세요 Tevo, 내 문제를 해결, 실제로 내 에뮬레이터를 다시 시작할 때 내 에뮬레이터를 다시 시작할 때 에뮬레이터에 문제가있었습니다. 응답에 대한 감사합니다. – Akshay

+0

이제 내 창에 이미지를 추가하고 싶습니다. 오른쪽 코드하지만 오류가 발생했습니다 Resources/main_folder/imgshop.png : 경로를 찾을 수 없습니다.이 문제를 해결할 수 있습니까? 감사합니다. Akshay – 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

관련 문제