Javascript iOS停止随机发送httprequests[钛]
我有一个应用程序,每5秒发送一次地理位置,如果没有找到新位置,每分钟发送一次最后找到的位置。因此,基本上应用程序无法停止向PHP文件发送地理位置信息 尽管如此,确实如此。完全是随机的。应用程序只是停止发送到服务器而没有出现错误(因为我的OneError中有一个警报),并且在我打开应用程序查看发生了什么时才开始重新发送 顺便说一句,它在Android上似乎工作得很好 我的info.plist中有一些东西可以让它保持活力(它确实让它保持活力,但我想它只是随意停止了活力):Javascript iOS停止随机发送httprequests[钛],javascript,ios,titanium,httprequest,appcelerator,Javascript,Ios,Titanium,Httprequest,Appcelerator,我有一个应用程序,每5秒发送一次地理位置,如果没有找到新位置,每分钟发送一次最后找到的位置。因此,基本上应用程序无法停止向PHP文件发送地理位置信息 尽管如此,确实如此。完全是随机的。应用程序只是停止发送到服务器而没有出现错误(因为我的OneError中有一个警报),并且在我打开应用程序查看发生了什么时才开始重新发送 顺便说一句,它在Android上似乎工作得很好 我的info.plist中有一些东西可以让它保持活力(它确实让它保持活力,但我想它只是随意停止了活力): ui背景模式 外部附件 位
ui背景模式
外部附件
位置
我的httprequest如下所示:
function sendCoordinates() {
//Reset the visual text(errors/succesmessage etc)
if (Titanium.Network.online) {
//Concat the GPSholder array into the toSend and than empty the GPSholder.
//To toSend accumulates GPSholder arrays in case it can't be sent for some reason but avoids getting duplicates in the GPSholder
//the toSend is emptied out after a succesful save.
toSend = toSend.concat(getGPSholder());
GPSholder = [];
if (toSend.length > 0) {
GPSSaved.text = '';
minuteInterval = 0;
var xhr=Titanium.Network.createHTTPClient({enableKeepAlive: false});
xhr.open("POST","http://xxx.nl/website/services/esrm_tracker/push_tt_positions.php");
xhr.onload = function(){
if(this.status == '200'){
if(this.readyState == 4){
var result = JSON.parse(this.responseText);
switch(result.result) {
case 1:
secondsLastSent = 0;
counterBlock.text = "De laatste locatie is " + secondsLastSent + " seconden geleden verstuurd";
counterBlock.show();
toSend = [];
break;
case -1:
GPSSaved.text = 'Authorisatie code niet geldig. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
case -2:
GPSSaved.text = 'Locaties niet geldig';
break;
case -3:
GPSSaved.text = 'Authorisatie code niet gevonden. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
case -10:
GPSstop();
break;
default:
GPSSaved.text = 'Onbekende fout. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
}
}
}
}
xhr.onerror = function(e){
GPSSaved.text = e.status + ' <- error';
alert(e);
};
xhr.timeout = 10000;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
var str = JSON.stringify(toSend);
var params = {
auth_key : auth_key,
locations : str
};
xhr.send(params);
}
} else {
GPSSaved.text = 'Geen internet. Het versturen van locaties wordt hervat als de verbinding is hervat.';
}
}
函数sendCoordinates(){
//重置可视文本(错误/成功消息等)
如果(钛网在线){
//将GPSholder数组连接到toSend中,然后清空GPSholder。
//To toSend累加GPSholder数组,以防由于某种原因无法发送,但避免在GPSholder中获得重复
//成功保存后,toSend将清空。
toSend=toSend.concat(getGPSholder());
GPSholder=[];
如果(toSend.length>0){
GPSSaved.text='';
分钟间隔=0;
var xhr=Titanium.Network.createHTTPClient({enableKeepAlive:false});
xhr.open(“POST”http://xxx.nl/website/services/esrm_tracker/push_tt_positions.php");
xhr.onload=函数(){
如果(this.status==“200”){
if(this.readyState==4){
var result=JSON.parse(this.responseText);
开关(result.result){
案例1:
secondsLastSent=0;
counterBlock.text=“De laatste locatie is”+secondsLastSent+“seconden geleden verstuurd”;
counterBlock.show();
toSend=[];
打破
案例1:
GPSSaved.text='授权代码niet geldig.Er worden geen locaties meer verstuurd';
GPSstop();
打破
案例2:
GPSSaved.text='Locaties niet geldig';
打破
案例3:
GPSSaved.text='授权代码niet gevonden.Er worden geen locaties meer verstuurd';
GPSstop();
打破
案例-10:
GPSstop();
打破
违约:
GPSSaved.text='Onbekende fout.Er worden geen locaties meer verstuurd';
GPSstop();
打破
}
}
}
}
xhr.onerror=函数(e){
GPSSaved.text=e.status+'从您的代码中可以看出,您没有为iOS注册后台任务。您只打算通过配置来完成此任务 我也不认为这是随机的,而是在应用程序关闭5分钟后。在这一点上,应用程序
会暂停
,直到再次打开,因此它将继续传输位置
您应该查看以下文档:
请注意,它将运行一个不同的实例,您不应该将整个应用程序包含在此后台服务中,因为它也没有UI。为它创建一个单独的文件,包括所需的http和数据库调用,仅此而已。作为一个弗里斯安人,我很乐意支持一个涉及
Elfstedentocht
的问题如果你遇到更多的问题,请告诉我谢谢你的提议。如果这个问题能够解决,我可能还有其他的事情要做,哈哈。尽管如此,我现在已经删除了这个链接,哈哈,我应该在后台服务中添加什么?只是那个功能?哦,顺便说一句。它现在在待机状态下运行大约20分钟,没有任何暂停问题。怎么了曾经。它只是每隔一分钟左右发送一次位置(它正在我的桌子上放置睡衣)。你需要创建一个文件,其中包含所有功能,你需要它才能正常工作,因为它不知道任何其他文件(除非你包括它们)。这包括所有依赖项。请定义“它”在您上次的回复中?我是否需要使用此httprequest内容以及该函数中使用的所有内容创建bgservice?是的。正是这样。它将作为一个独立的应用程序。因此,为了使您的任务正常工作,它需要js文件中的所有依赖项
function sendCoordinates() {
//Reset the visual text(errors/succesmessage etc)
if (Titanium.Network.online) {
//Concat the GPSholder array into the toSend and than empty the GPSholder.
//To toSend accumulates GPSholder arrays in case it can't be sent for some reason but avoids getting duplicates in the GPSholder
//the toSend is emptied out after a succesful save.
toSend = toSend.concat(getGPSholder());
GPSholder = [];
if (toSend.length > 0) {
GPSSaved.text = '';
minuteInterval = 0;
var xhr=Titanium.Network.createHTTPClient({enableKeepAlive: false});
xhr.open("POST","http://xxx.nl/website/services/esrm_tracker/push_tt_positions.php");
xhr.onload = function(){
if(this.status == '200'){
if(this.readyState == 4){
var result = JSON.parse(this.responseText);
switch(result.result) {
case 1:
secondsLastSent = 0;
counterBlock.text = "De laatste locatie is " + secondsLastSent + " seconden geleden verstuurd";
counterBlock.show();
toSend = [];
break;
case -1:
GPSSaved.text = 'Authorisatie code niet geldig. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
case -2:
GPSSaved.text = 'Locaties niet geldig';
break;
case -3:
GPSSaved.text = 'Authorisatie code niet gevonden. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
case -10:
GPSstop();
break;
default:
GPSSaved.text = 'Onbekende fout. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
}
}
}
}
xhr.onerror = function(e){
GPSSaved.text = e.status + ' <- error';
alert(e);
};
xhr.timeout = 10000;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
var str = JSON.stringify(toSend);
var params = {
auth_key : auth_key,
locations : str
};
xhr.send(params);
}
} else {
GPSSaved.text = 'Geen internet. Het versturen van locaties wordt hervat als de verbinding is hervat.';
}
}
var service = Ti.App.iOS.registerBackgroundService({url:'bg-service1.js'});