【问题标题】:Titanium, Wait for HTTPClient replyTitanium,等待 HTTPClient 回复
【发布时间】:2013-10-18 23:26:36
【问题描述】:

我正在使用 Titanium SDK 开发一个小型 Android 应用程序,该应用程序与远程 PHP 文件交互以检索其数据。 FOR 循环在 HTTPClient 返回任何数据之前执行,因此“myTest”为空,并且没有任何内容添加到“tblListing”。

function jsonPOST( inAction, inParams ) { // jsonPOST is a global function
    var xhr = Ti.Network.createHTTPClient({
        onload : function(e) {
            Ti.API.info("Received text: " + this.responseText);
            return this.responseText;
        },
        onerror : function(e) {
            Ti.API.debug(e.error);
            alert('error');
            return false;
        },
        timeout : 8000,  // in milliseconds
    });

    var sendData = {
        'action' : inAction,
        'json' : JSON.stringify(inParams)
    };

    xhr.open('POST', "http://domain.com/file.php"); // url redacted
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send( sendData );
} // end. jsonPOST()

var myTest = jsonPOST('getlisting'); // only need to pass first param in this instance
for (i in myTest) {
    tblListing.appendRow({ title: myTest[i].title, id: myTest[i].id });
}

在不延迟同一线程上其他任何内容的执行的情况下,如何使 FOR 循环等待直到 HTTPClient 返回数据? 'jsonPOST' 函数用于检索应用中多个元素的各种数据,并且应该保持动态。

【问题讨论】:

    标签: android titanium httpclient


    【解决方案1】:

    我最终使用回调参数来允许在 HTTPClient 接收到数据后调用函数。这允许 jsonPOST 函数保持动态。

        function jsonPOST(inAction, inParams, inCallback) {
    
            var xhr = Ti.Network.createHTTPClient({
                onload : function(e) {
                    Ti.API.info("Received text: " + this.responseText);
                    var reply = JSON.parse(this.responseText);
                    inCallback(reply);
                },
                onerror : function(e) {
                    Ti.API.debug(e.error);
                    alert('error');
    
                    return false;
                },
                timeout : 8000,  // in milliseconds
            });
    
            var sendData = {
                'action' : inAction,
                'json' : JSON.stringify(inParams)
            };
            xhr.open('POST', "http://domain.com/file.php"); // url redacted
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(sendData);
        }
    
    function processListing(inJson) {
        for (i in inJson) {
            tblListing.appendRow({
                title : inJson[i].listingTitle,
                id : inJson[i].listingID
            });
        }
    }
    
    jsonPOST('getListing', null, processListing);
    

    【讨论】:

      【解决方案2】:

      循环需要在绑定到 xhr 对象的onload 属性的函数中:

       function jsonPOST( inAction, inParams ) { // jsonPOST is a global function
          var xhr = Ti.Network.createHTTPClient({
              onload : function(e) {
                  Ti.API.info("Received text: " + this.responseText);
                  for (i in this.responseText) {
                    tblListing.appendRow({ title: myTest[i].title, id: myTest[i].id });
                  }
                  return this.responseText;
              },
              onerror : function(e) {
                  Ti.API.debug(e.error);
                  alert('error');
                  return false;
              },
              timeout : 8000,  // in milliseconds
          });
      
          var sendData = {
              'action' : inAction,
              'json' : JSON.stringify(inParams)
          };
      
          xhr.open('POST', "http://domain.com/file.php"); // url redacted
          xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          xhr.send( sendData );
      } // end. jsonPOST()
      
      var myTest = jsonPOST('getlisting'); // only need to pass first param in this instance
      

      请注意,HTTPClient 是异步工作的。它发送请求并等待数据,但同时调用脚本继续执行。

      【讨论】:

        猜你喜欢
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多