我在Titanium Studio中构建一个Android应用程序,并使用Rest api(Apigee Baas)作为后端组件。我在Apigee Baas中有一些数据,例如:姓名,地址,出勤率等。
首先在我的代码中,我以以下方式从Rest API得到响应。
"entities" : [ {
"uuid" : "8780273a-d210-11e4-81a4-cba5dc698934",
"type" : "attendance",
"created" : 1427192916259,
"modified" : 1427192916259,
"attendance" : [ {
"date" : "10-09-1991",
"atn" : "0"
} ],
"disputes" : "None",
"metadata" : {
"path" : "/attendances/8780273a-d210-11e4-81a4-cba5dc698934"
},
"password" : "1122",
"servicepro" : "pqrst",
"username" : "priyankaa"
} ],//json object response
我已经在Titanium Studio中尝试了以下代码。
index.js file
function doClick(e){
var url = "api.usergrid.com/PRI_95616/LOGIN/attendances?ql=select * where
username= '"+$.usertf.value.toString()+"'
and password='"+$.passtf.value.toString()+"'";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
var params={};
var json=JSON.parse(this.responseText);
Ti.API.info('resp is'+this.responseText);
var user=json.entities[0].username;
params["username"]=user;
var pass=json.entities[0].password;
params["password"]=pass;
var servp=json.entities[0].servicepro;
params["servicepro"]=servp;
var atnd=json.entities[0].attendance[0].atn;
params["attendance"]=atnd;
Ti.API.info('atn'+atnd);
var atndate=json.entities[0].attendance[0].date;
params["attendance"]=atndate;
Ti.API.info('atn'+atndate);
if((user==$.usertf.value.toString() && pass==$.passtf.value.toString())
{
var window = Alloy.createController('profile', params).getView();
window.open();
}
else{
alert('error');
};
},
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout : 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();//http request
}
$.index.open();
这里的出勤率是两个对象atn和date的数组,存储在apigee低音中。现在,我正在获取考勤值,但是当我将此考勤值(即数组)使用参数传递给另一个控制器时,无法在其中访问它。其他值,例如,正确访问了用户名地址。仅访问值不会被访问。下面是我的profile.js文件。这是我访问这些值的另一个控制器。
profile.js
var args = arguments[0] || {};
$.tff.text=args.username;
$.sertf.text=args.servicepro;
$.prevtf.text=args.attendance[0];
Ti.API.info('ath prof: '+args.attendance.atn);
Ti.API.info('ath date prof: '+args.date);
Ti.API.info('atnd:'+args.attendance[0].atn);
在profile.js中,无法访问阵列的出勤率。如何访问这些数组元素有人可以帮帮我吗
如果您要更改,则不是attendance
从阵列发送index.js
并尝试访问它profile.js
var atndate=json.entities[0].attendance[0].date;
params["attendance"]=atndate;
到
var atndate=json.entities[0].attendance[0];
params["attendance"]=atndate;
我认为它将起作用。
同样,您正在覆盖params["attendance"]
您的代码。
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句