Titanium:在尝试从远程服务器访问json数据时给出错误并且没有任何进入

时间:2014-02-27 10:35:08

标签: titanium titanium-alloy

您好我是Titanium移动应用程序的新手我试图从远程服务器获取数据但失败了。我正在使用Titanium Studio,版本:3.2.1.201402041146。

这是我的index.xml文件

<Alloy>
    <TabGroup id="mainTabGroup">
        <!-- On click event execute getTodoList -->
        <Tab id="tab1" onClick="getTodoList">
            <Window id="readWin">
                <TableView id="tableView"/>
            </Window>
        </Tab>
        <Tab id="tab2">
            <Window id="insertWin">
                <View id="mainView">
                    <TextField id="inserTxtF"/>
                    <Button id="insertBtn" onClick="insertData" />
                </View>
            </Window>
        </Tab>
    </TabGroup>
  </Alloy>

/////这是我的index.js文件代码////

//Array to store the data from the todo list

 var dataArray = [];

//We execute the function to show the data for the first view

    getTodoList();


function getTodoList () {

    var sendit = Ti.Network.createHTTPClient({
        onerror: function(e){
            Ti.API.debug(e.error);
            alert('There was an error during the conexion');
        },
        timeout:1000,
    });

    //Here you have to change it for your local ip

    sendit.open('GET', 'http://attarisoft.com/read_todo_list.php');  
    sendit.send(); 
    //Function to be called upon a successful response
    sendit.onload = function(){
        var json = JSON.parse(this.responseText);
        var json = json.todo;
        //if the database is empty show an alert
        if(json.length == 0){
            $.tableView.headerTitle = "The database row is empty";
        }

        //Emptying the data to refresh the view
        dataArray = [];

        //Insert the JSON data to the table view
        for( var i=0; i<json.length; i++){
            var row = Ti.UI.createTableViewRow({
                title: json[i].todo,
                hasChild : true,
            });     
            dataArray.push(row);                
        };

        $.tableView.setData(dataArray);

     };
  }

////这是提示警告消息conexion期间出现错误,表格视图中没有任何显示请帮助

3 个答案:

答案 0 :(得分:0)

index.js

  function getTodoList(){
    var sendit = Ti.Network.createHTTPClient({

        onload : function(e){
          var json = JSON.parse(this.responseText);
          Ti.API.info(json);//Check your data to return

          var json = json.todo;
          //if the database is empty show an alert
          if(json.length == 0){
              $.tableView.headerTitle = "The database row is empty";
          }

          //Emptying the data to refresh the view
          dataArray = [];

          //Insert the JSON data to the table view
          for( var i=0; i<json.length; i++){
              var row = Ti.UI.createTableViewRow({
                  title: json[i].todo,
                  hasChild : true,
              });     
              dataArray.push(row);                
          };

          $.tableView.setData(dataArray);

        },
        onerror: function(e){
            Ti.API.info(e.error);
            alert('There was an error during the conexion');
        },
        timeout:5000, 
});

//Here you have to change it for your local ip

sendit.open('GET', 'http://attarisoft.com/read_todo_list.php');  
sendit.send();
}
$.mainTabGroup.open();

INDEX.XML

<Alloy>
<TabGroup id="mainTabGroup">
    <!-- On click event execute getTodoList -->
    <Tab id="tab1"  title="Main" onFocus="getTodoList">
        <Window id="readWin">
            <TableView id="tableView"/>
        </Window>
    </Tab>
    <Tab id="tab2" title="Second">
        <Window id="insertWin">
            <View id="mainView">
                <TextField id="inserTxtF"/>
                <Button id="insertBtn" />
            </View>
        </Window>
    </Tab>
</TabGroup>

问题是标签事件。 OnClick - &gt; OnFocus for Tab

答案 1 :(得分:0)

我认为你在这里遇到了一些问题:

  • 首先,在您的代码中,您在设置回调函数之前发送请求。虽然这应该有用,但不确定它是否会表现得像你想要的那样。
  • 通过明确设置错误和超时(而不是在创建时分配这些值),我在这些请求方面取得了更大的成功。

话虽如此,请尝试使用此代码:

function getTodoList () {

var sendit = Ti.Network.createHTTPClient();

//Here you have to change it for your local ip

sendit.open('GET', 'http://attarisoft.com/read_todo_list.php');  
//Function to be called upon a successful response
sendit.onload = function(){
    var json = JSON.parse(this.responseText);
    var json = json.todo;
    //if the database is empty show an alert
    if(json.length == 0){
        $.tableView.headerTitle = "The database row is empty";
    }

    //Emptying the data to refresh the view
    dataArray = [];

    //Insert the JSON data to the table view
    for( var i=0; i<json.length; i++){
        var row = Ti.UI.createTableViewRow({
            title: json[i].todo,
            hasChild : true,
        });     
        dataArray.push(row);                
    };

    $.tableView.setData(dataArray);

 };
 sendit.onerror = function(){
        Ti.API.debug(e.error);
        alert('There was an error during the conexion');
 };
 sendit.setTimeout(10000); // 10 sec for timeout
 sendit.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 sendit.send();
 }

最后一条建议,最好创建一个单独的模块来处理这些请求(假设你不止一次这样做)。 如果这仍然无法解决您的问题,请回复您的新错误。 希望它有所帮助!

答案 2 :(得分:0)

修复如下

            var client = new XMLHttpRequest();
    client.open("GET", "http://www.domainname.com/read_todo_list.php", true);
    client.send();
    client.onreadystatechange = function(){

    if (client.readyState==4 && client.status==200)
        {
        //console.log(JSON.parse(client.response));
        //json = JSON.stringify(client.response);
        var get=JSON.parse(client.response);
        console.log(get['todo']);
        for( var i=0; i < get['todo'].length; i++){

        var row = Ti.UI.createTableViewRow({
            title: get['todo'][i].todo,
            hasChild : true,
            });     
            dataArray.push(row);                
        }

        $.tableView.setData(dataArray);

        }




    };