从Titanium中的网页获取数据

Get data from webpage in Titanium

本文关键字:获取 数据 网页 Titanium      更新时间:2023-09-26

我用titanium编写了一小段代码,用于从网页中获取数据。但是,当我运行应用程序并按下将触发该函数的按钮时,它不会显示数据。

有人能向我解释我做错了什么,为什么我做错了吗?

这是我的代码:

// include needed files
Ti.include('responsive.js');
//Ti.include('http_connection.js');

//Create the screen
//The home screen
var homeWindow = Ti.UI.createWindow({
  exitOnClose: true,
  fullscreen: false,
  title: 'Advanced'
});
var homeView = Ti.UI.createView({
  backgroundColor: 'white'
});
var homeLabel = Ti.UI.createLabel({
    top: 20,
    left: 30,
    height: 30,
    text: 'Test text',
    color: 'black',
    font: {fontSize: 18}
});
var testButton1 = Ti.UI.createButton({
    title: 'test',
    backgroundColor: 'red',
    top: 55,
    left: per10,
    width:per60,
    height: 30,
    color: 'black',
    font: {fontSize: 14} 
});
var testButton2 = Ti.UI.createButton({
    title: 'test2',
    backgroundColor: 'blue',
    top: 95,
    left: per10,
    width:per60,
    height: 30,
    color: 'black',
    font: {fontSize: 14} 
});
testButton2.addEventListener('click',function(e){
    Ti.API.info("Button Clicked");
    http_con();
    Ti.API.info("Button Clicked 2");
    //alert('test');
});
function http_con() {
    Ti.API.info('hya');
    //Database connection
    var http_client = Ti.Network.createHTTPClient();
    http_client.open('POST', 'http://rdbomers-hp:89/ceres');
    //If variables has been send
    http_client.onload = function() {
        Ti.API.info('subjects: ' + this.responseText);
        callback(this.responseText);
    };
    //If there is an error
    http_client.onerror = function(e) {
        Ti.API.info('error: ' + JSON.stringify(e));
    };
};
//Creating the application
//Home screen
homeWindow.add(homeView);
homeView.add(homeLabel);
homeView.add(testButton1);
homeView.add(testButton2);
homeWindow.open();

我已经在函数中放入了Ti.API.info('hya'),以检查它是否到达那里,它显示HYA,但我希望它显示网页的内容。

您应该在调用.open之前定义回调,就像这个

function http_con() {
    Ti.API.info('hya');
    //Database connection
    var http_client = Ti.Network.createHTTPClient();
    //If variables has been send
    http_client.onload = function() {
        Ti.API.info('subjects: ' + this.responseText);
        callback(this.responseText);
    };
    //If there is an error
    http_client.onerror = function(e) {
        Ti.API.info('error: ' + JSON.stringify(e));
    };
    http_client.open('POST', 'http://rdbomers-hp:89/ceres');
};