2012-02-10 19 views
1

朋友你好的onload功能,鈦手機:所謂的問題JSON解析

我正在開發使用谷歌API地方,所以我使用JSON解析顯示ATM類名單的tableview &地址鈦工作室SDK 1.8.1應用程序使用此link,但getData方法的loader.onload函數不會在json解析中的getData方法的send函數後立即調用,因此它在getDetailsData()函數之後調用,並且也無法在tableview中顯示地址,所以請給我介紹如何解決這個問題。

在此先感謝。

var lat ,lon ,radius , name , sensor , key , reference, address; 
lat = '-33.8670522';//'23.042067'; 
lon = '151.1957362';//'72.530835';// 
radius = '500'; 
name = title; 
sensor = 'false'; 
key = 'AIzaSyDALrXHC4uMtfSrpCg6NHxqPhsLccLYPZE'; 

var rowData = []; 

// getCategoryData using Google Place API 
function getData() 
{ 
    var loader = Titanium.Network.createHTTPClient(); 

    var url = "https://maps.googleapis.com/maps/api/place/search/json?";  
    url = url + "location=" + lat + ',' + lon; 
    url = url + "&radius=" + radius; 
    url = url + "&name=" + name; 
    url = url + "&sensor=" + sensor; 
    url = url + "&key=" + key; 

    Ti.API.info(url); 
    // Sets the HTTP request method, and the URL to get data from 
    loader.open("GET",url); 
    // Create our HTTP Client and name it "loader" 
    // Runs the function when the data is ready for us to process 
    loader.onload = function() 
    { 
     var obj = JSON.parse(this.responseText); 
     Ti.API.log(obj);  
     var results = obj.results; 
     Ti.API.log(results); 
     for (var i = 0; i < results.length; i++) 
     { 
      var name = obj.results[i].name; 
      reference = obj.results[i].reference; 
      Ti.API.log('Refernce:'+reference); 

        getDetailsData(); 

      // Create a row and set its height to auto 
      var row = Titanium.UI.createTableViewRow({height:'auto'}); 

      // Create the view that will contain the text and avatar 
      var post_view = Titanium.UI.createView({ 
       height:'auto', 
       layout:'vertical', 
       top:5, 
       right:5, 
       bottom:5, 
       left:5 
      }); 
       // Create the label to hold the tweet message 
      var nameLabel = Titanium.UI.createLabel({ 
       //text:name, 
       left:30, 
       top:0, 
       bottom:2, 
       height:'auto', 
       width:236, 
       textAlign:'left', 
       font:{fontSize:14} 
      }); 

      // Create the label to hold the tweet message 
      var addressLabel = Titanium.UI.createLabel({ 
       text:'Address', 
       left:30, 
       top:0, 
       bottom:2, 
       height:'auto', 
       width:236, 
       textAlign:'left', 
       font:{fontSize:14} 
      }); 

      nameLabel.text = name; 
      //addressLabel.text = placeAddress; 

      post_view.add(nameLabel); 
      post_view.add(addressLabel); 

      // Add the post view to the row 
      row.add(post_view); 
      // Give each row a class name 
      //row.className = "item"+i; 
      // Add row to the rowData array 
      rowData[i] = row; 
      //rowData.push(row); 
     } 

     //tableView.setData(rowData); 
     // Create the table view and set its data source to "rowData" array 
     var tableView = Titanium.UI.createTableView({data:rowData}); 
     //Add the table view to the window 
     showWin.add(tableView); 
    }; 
    //-- Network error 
    loader.onerror = function(e) 
    { 
     Ti.API.info('Network error: ' + JSON.stringify(e)); 
    }; 


    // Send the HTTP request 
    loader.send(); 
} 




function getDetailsData() 
{ 
    var loader1 = Titanium.Network.createHTTPClient(); 

    Ti.API.log('getDetailsData'); 
    var url = "https://maps.googleapis.com/maps/api/place/details/json?";  
    url = url + "reference=" + reference; 
    url = url + "&sensor=" + sensor; 
    url = url + "&key=" + key; 
    Ti.API.info(url); 

    // Sets the HTTP request method, and the URL to get data from 
    loader1.open("GET",url); 

    // Runs the function when the data is ready for us to process 
    loader1.onload = function() 
    { 
     var detailsObj = JSON.parse(this.responseText); 
     Ti.API.log(detailsObj); 

     address = detailsObj.result.formatted_address; 
     Ti.API.log('Address:'+address); 

     phoneno = detailsObj.result.formatted_phone_number; 
     Ti.API.log('Phone No:'+phoneno); 
    }; 

    //-- Network error 
    loader1.onerror = function(event) 
    { 
     Ti.API.info('Network error: ' + JSON.stringify(event)); 
    }; 

    // Send the HTTP request 
    loader1.send(); 

    return address; 
} 

getData(); 

enter image description hereenter image description here

+0

你想在哪裏顯示地址?在第二或第一個屏幕? – 2012-02-10 11:59:40

+0

在每個atm名下面的第二個屏幕中 – 2012-02-10 12:03:04

+0

你能解釋一下兩個http請求的流程嗎?你爲什麼使用兩個請求? – 2012-02-10 12:24:10

回答

1

Dont't使用在所述第二HTTP請求中返回。 通過函數中的標籤對象,例如:

getDetailsData(addressLabel);

,並設置內部loader1.onload文本這樣的:

地址= detailsObj.result.formatted_address;

addressLabel.text = address;

+0

plz在線聊天http://chat.stackoverflow.com/rooms/7529/discussion-between-muhammad-zeeshan-and-nikunj-r-jadav – 2012-02-28 04:38:53

+0

plz在線聊天http://chat.stackoverflow .COM /間/ 7529 /討論之間,穆罕默德zeeshan-和nikunj-R-jadav – 2012-03-07 05:12:56