HTTPClient 调用未在 Appcelerator Titanium 应用程序中返回正确的 JSON 数据
HTTPClient call is not returning correct JSON data in Appcelerator Titanium app
每次我尝试从 JSON 获取我的信息时,我都会收到错误消息。
function buscar(e){
var url = 'https://www.dotscancun.com/createjson.php?id=100001';
var xhr = Ti.Network.HTTPClient({
onerror: function(e){
Ti.API.info(this.responseText);
Ti.API.info(this.status);
Ti.API.info(e.error);
},
timeout: 5000
});
xhr.open('GET',url);
xhr.send();
xhr.onload = function(){
var json = JSON.parse(this.responseText);
alert(json);
};
};
这是代码。
错误是:
[LiveView] Client connected
[ERROR] : TiHTTPClient: (TiHttpClient-8) [1340,1340] HTTP Error (java.io.IOException): 404 : Not Found
[ERROR] : TiHTTPClient: java.io.IOException: 404 : Not Found
[ERROR] : TiHTTPClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1217)
[ERROR] : TiHTTPClient: at java.lang.Thread.run(Thread.java:818)
错误 404 表示该网站不存在,但是如果您复制 url 它可以工作,请问有什么问题吗?
您在问题中发布的错误消息与您的 JSON 查询无关。相反,它与您的 Android 设备日志输出有关。所以你可以简单地忽略那个电话。
您编码错误是因为:
- 您没有创建正确的 HTTPClient 对象。您正在使用 Ti.Network.HTTPClient 而不是 Ti.Network.createHTTPClient
- onload 方法应该在 open() 调用之前定义。
这是您问题的正确代码:
function buscar(e){
var url = 'https://www.dotscancun.com/createjson.php?id=100001';
var xhr = Ti.Network.createHTTPClient({
onerror: function(e){
Ti.API.info(this.responseText);
Ti.API.info(this.status);
Ti.API.info(e.error);
},
timeout: 5000,
onload : function(){
alert(this.responseText);
// parse it for further use
var tempJSON = JSON.parse(this.responseText);
}
});
xhr.open('GET',url);
xhr.send();
}
每次我尝试从 JSON 获取我的信息时,我都会收到错误消息。
function buscar(e){
var url = 'https://www.dotscancun.com/createjson.php?id=100001';
var xhr = Ti.Network.HTTPClient({
onerror: function(e){
Ti.API.info(this.responseText);
Ti.API.info(this.status);
Ti.API.info(e.error);
},
timeout: 5000
});
xhr.open('GET',url);
xhr.send();
xhr.onload = function(){
var json = JSON.parse(this.responseText);
alert(json);
};
};
这是代码。
错误是:
[LiveView] Client connected
[ERROR] : TiHTTPClient: (TiHttpClient-8) [1340,1340] HTTP Error (java.io.IOException): 404 : Not Found
[ERROR] : TiHTTPClient: java.io.IOException: 404 : Not Found
[ERROR] : TiHTTPClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1217)
[ERROR] : TiHTTPClient: at java.lang.Thread.run(Thread.java:818)
错误 404 表示该网站不存在,但是如果您复制 url 它可以工作,请问有什么问题吗?
您在问题中发布的错误消息与您的 JSON 查询无关。相反,它与您的 Android 设备日志输出有关。所以你可以简单地忽略那个电话。
您编码错误是因为:
- 您没有创建正确的 HTTPClient 对象。您正在使用 Ti.Network.HTTPClient 而不是 Ti.Network.createHTTPClient
- onload 方法应该在 open() 调用之前定义。
这是您问题的正确代码:
function buscar(e){
var url = 'https://www.dotscancun.com/createjson.php?id=100001';
var xhr = Ti.Network.createHTTPClient({
onerror: function(e){
Ti.API.info(this.responseText);
Ti.API.info(this.status);
Ti.API.info(e.error);
},
timeout: 5000,
onload : function(){
alert(this.responseText);
// parse it for further use
var tempJSON = JSON.parse(this.responseText);
}
});
xhr.open('GET',url);
xhr.send();
}