钛移动:onload函数在json解析中调用问题

发布于 2025-01-04 11:30:33 字数 5057 浏览 1 评论 0原文

大家好,

我正在 Titanium Studio sdk 1.8.1 中使用 Google Place API 开发一个应用程序来显示类别 atm 列表和信息。地址在表视图中,所以我使用这个 链接 但是json解析中getData方法的loader.onload函数并没有在getData方法的send函数之后立即调用,所以在getDetailsData()函数之后调用,也无法显示地址在表格视图中,所以请告诉我如何解决它。

提前致谢。

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();

在此处输入图像描述 在此输入图像描述

Hello friends,

I am developing an app in Titanium Studio sdk 1.8.1 using Google Place API to display category atm list & address in tableview so I use json parsing using this link but loader.onload function of getData method is not called immediately after send function of getData method in json parsing so its called after getDetailsData() function and also can't display address in tableview so please give me idea how to solve it.

Thanks in advance.

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 here enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2025-01-11 11:30:33

不要在第二个 http 请求中使用返回值。
在函数中传递标签对象,如下所示:

getDetailsData(addressLabel);

并在 loader1.onload 中设置文本,如下所示:

地址=detailsObj.result.formatted_address;

addressLabel.text = 地址;

Dont't use the return in the second http request.
Pass the label object in the function like:

getDetailsData(addressLabel);

and set the text inside loader1.onload like this:

address = detailsObj.result.formatted_address;

addressLabel.text = address;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文