【发布时间】:2014-05-18 17:18:43
【问题描述】:
我是应用程序开发的新手。我正在使用 Intel XDK 制作 Android 应用。
该应用程序通过连接到使用 Symfony 完成的后端中的数据库来工作,并且在模拟器中表现良好。我现在想通过 WiFi 使用我的 PC 作为 localhost 从真实设备进行测试。我读到您必须使用 ipconfig 检索本地主机 IP 地址,然后使用该 IP 进行连接。但是,当我测试应用程序时,它没有连接。奇怪的是,如果我使用具有相同 URL 的移动浏览器,则连接正常:我可以看到用于获取数据库信息的 XML 文件。
应用中的连接代码:
xmlhttp=new XMLHttpRequest();
xmlhttp.onerror=transferFailed;
xmlhttp.open("GET","http://192.xxx.x.x/symfony/web/app_dev.php/api/v1/reportes/reportes.xml",false);
onerror函数:
function transferFailed(e) {
alert("Error al conectar:"+e.target.status);
}
正如我所说,连接不起作用,我收到警告消息:“Error al conectar: 0”
如果我将地址 http://192.xxx.x.x/symfony/web/app_dev.php/api/v1/reportes/reportes.xml 输入移动浏览器,我会得到正确的 XML 文件。
编辑: 看来问题出在 sycnhronus 请求上。后来我不得不解析响应xml。我不知道为什么在模拟器和我前段时间在 phonegap 中制作的应用程序中它可以在没有解析的情况下工作。代码:
function mapIni() {
xmlhttp=new XMLHttpRequest();
xmlhttp.onerror=transferFailed;
xmlhttp.open("GET","http://localhost/symfony/web//app_dev.php/api/v1/reportes/reporte.xml",true);
xmlhttp.onload= function (event) {
alert("onload:"+xmlhttp.response);
};
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
xmlDoc=xmlhttp.responseXML;
xmlDoc=xmlhttp.responseXML;
xmlDoc = new DOMParser().parseFromString(xmlhttp.responseText,'text/xml');
x=xmlDoc.getElementsByTagName("entry");
alert("x[0]:"+x[0].getElementsByTagName("tipo")[0].childNodes[0].nodeValue);
}
}
xmlhttp.send();
}
【问题讨论】: