无法在Titanium studio的android应用程序中连接到JSON服务
我正在尝试执行需要 id 和密码的登录应用程序..当我单击 logi 按钮时,它将通过 JSON 连接到我们的本地服务器..使用指定的 URL..代码是..
var loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function()
{
var json = this.responseText; alert(json);
var response = JSON.parse(json);
if (response.data.status == "success")
{ alert("Welcome ");
}
else
{ alert(response.data.status);
}
};
loginReq.onerror = function(event)
{
alert(event.toSource());
//alert("Network error");
};
loginBtn.addEventListener('click',function(e)
{ if (username.value != '' && password.value != '')
{
var url = 'our local url action=login&id='+username.value+'&pwd='+password.value;
loginReq.open("POST",url);
loginReq.send();
}
else
{
alert("Username/Password are required");
}
});
这里它没有连接我们的URl..所以它进入了loginReq.onerror函数...而不是loginReq.onload函数..为什么它抛出运行时错误..相同的代码在Iphone上运行良好.. 运行时错误是.. TypeError:Cannot call property toSource in object{'source':[Ti.Network.HttpClient],specified url} 不是一个函数,它是一个对象。
这是错误......请让我知道......
i am trying to do login application which takes id and password..when i click on logi button then it will connect to our local server by JSON..with the specified URL..the code is..
var loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function()
{
var json = this.responseText; alert(json);
var response = JSON.parse(json);
if (response.data.status == "success")
{ alert("Welcome ");
}
else
{ alert(response.data.status);
}
};
loginReq.onerror = function(event)
{
alert(event.toSource());
//alert("Network error");
};
loginBtn.addEventListener('click',function(e)
{ if (username.value != '' && password.value != '')
{
var url = 'our local url action=login&id='+username.value+'&pwd='+password.value;
loginReq.open("POST",url);
loginReq.send();
}
else
{
alert("Username/Password are required");
}
});
Here it is not connecting our URl..so it is entering into loginReq.onerror function...instead of loginReq.onload function..why it is throwing run time error.. The same code working fine with Iphone..
The Run Time Error is..
TypeError:Cannot call property toSource in object{'source':[Ti.Network.HttpClient],specified url} is not a function,it is a object.
This is wat the error..please let me Know...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然 android 中不存在 toSource() 函数,因为它是一个对象。尝试调试并查看对象事件包含什么。
您可以通过在警报行上方添加一行并向其添加调试行来实现此目的。
在调试模式下查看并查看所有变量
Apparently the toSource() function does not exist in android, as it is an object. Try debugging and see what the object event contains.
You could do that by adding a line above the alert line, and adding a debug line to it.
Look in debug mode and see all variables
“toSource()”不是这两个平台的记录函数,我也没有在 Titanium Mobile 的源代码中看到它。如果您在 iOS 上没有收到错误,我猜这是因为错误处理程序没有被调用。也许您的模拟器或设备无法访问互联网,而您的 iOS 模拟器或设备却可以访问互联网?
无论如何,HTTPClient 中的错误处理通常如下所示:
"toSource()" is not a documented function for either platform, and I also do not see it in the source for Titanium Mobile. If you aren't getting the error on iOS, I'm guessing it is because the error handler isn't getting called. Perhaps your emulator or device does not have internet access, whereas your iOS simulator or device does?
Regardless, error handling in the HTTPClient normally looks something like this: