【问题标题】:Retrieving of Restful web service values in android for Titanium在 android 中为 Titanium 检索 Restful Web 服务值
【发布时间】:2016-05-26 04:56:33
【问题描述】:

我们为 android 和 ios 使用来自 serviceutility.js 的相同的 RESTful Web 服务代码。但是该服务受到打击,并且仅在 ios 中检索值。相同的代码在 android 中不起作用,我们收到以下错误:

[ERROR] : TiExceptionHandler: (main) [2,821093] - 在alloy/controllers/home.js:25,32

[ERROR] : TiExceptionHandler: (main) [0,821093] - 消息:未捕获的 TypeError:无法读取 null 的属性“状态”

[ERROR] : TiExceptionHandler: (main) [0,821093] - 来源:if ("1" == response.status) alert(response.message);否则如果(“0”

[错误]:V8Exception:Alloy/controllers/home.js:25 发生异常:未捕获类型错误:无法读取 null 的属性“状态”。

Titanium SDK 是 5.1.2 GA

exports.login = function(user, cb) {
var response = null;
if (Ti.Network.online) {
    var xhr = Ti.Network.createHTTPClient({
        timeout : 10000,
        validatesSecureCertificate : false
    });
    xhr.onload = function() {// Onload
        var responseTxt = this.responseText == '' ? '{}' : this.responseText;
        try {
            response = JSON.parse(responseTxt);
            cb(response, 'SUCCESS');
        } catch(e) {
            cb(response, 'ERROR');
        }
    };
    xhr.onerror = function(e) {
        if (xhr.status === 0) {
            cb(response, 'TIMEDOUT');
        } else {
            cb(response, 'ERROR');
        }
    };
    url = "https://";
    var postData = {
        employeeId : user.employeeId,
        password : user.password
    };
    xhr.open('POST', url);
    xhr.setTimeout(10000);
    xhr.setRequestHeader('employeeId', user.employeeId);
    xhr.setRequestHeader('password', user.password);
    xhr.send();} else {
            cb(response, 'NO_NETWORK');
}};

下面的代码是 index.js 文件的实际检索值发生的地方。

if (Ti.Network.online) {
        loginUtil.login(user, function(response, status) {
            Ti.API.info("status----" + status);
            if (response.status == "0") {
                Ti.API.info("status== " + response.status);
                Ti.App.role = response.role;
                Alloy.createController('home',  {employeeId:$.userTextField.value,password:$.passwordTextField.value,from:"index"}).getView().open();
                
            } else if (response.status == '1') {

                alert(response.message);

            } else {

                alert("Please enter the correct credentials");
            }
        });
    }

请帮助我们。

【问题讨论】:

  • 您需要显示一些代码... API 调用的成功回调中包含什么,因为这是一个常规的 javascript 错误
  • .. 或尝试 Reste:github.com/jasonkneen/RESTe
  • 嗨,Rene,感谢您的回复。我通过添加编码部分来编辑我的问题。请参考。

标签: appcelerator appcelerator-titanium


【解决方案1】:

您的回调返回两个参数,响应和状态,第二个参数从未使用过。

通过读取登录功能代码,您只有在 status == "SUCCESS" 时才能访问响应对象

if(status === "SUCCESS"){
    if (response.status == "0") {
            Ti.API.info("status== " + response.status);
            Ti.App.role = response.role;
            Alloy.createController('home',  {employeeId:$.userTextField.value,password:$.passwordTextField.value,from:"index"}).getView().open();

    } else if (response.status == '1') {

            alert(response.message);

    } else {

            alert("Please enter the correct credentials");
    }
}
else {
    alert("whoops, please try again !"); // a more generic message.
}

【讨论】:

    【解决方案2】:

    您的 index.js 期望 response 是一个对象,但这只是您像这样调用 callback 的情况:

            response = JSON.parse(responseTxt);
            cb(response, 'SUCCESS');
    

    您调用callback response 变量的所有其他地方都是null,因为这是您在第二行初始化它的地方。

    【讨论】:

      【解决方案3】:

      看起来您只返回一个字符串值而不是整个响应对象。然后在您的控制器中尝试访问响应对象的 .status 属性。

      //this line returns the string responseTxt
      response = JSON.parse(responseTxt);
      

      尝试返回整个响应对象。

      response = JSON.parse(this);
      

      然后在你的 index.js 控制器中使用/显示状态属性

      alert(response.status);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-09
        • 1970-01-01
        • 1970-01-01
        • 2016-02-18
        • 2012-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多