Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 与HTML5应用程序相比,带有电话间隙的Android应用程序无法正常工作_Javascript_Android_Json_Html_Cordova - Fatal编程技术网

Javascript 与HTML5应用程序相比,带有电话间隙的Android应用程序无法正常工作

Javascript 与HTML5应用程序相比,带有电话间隙的Android应用程序无法正常工作,javascript,android,json,html,cordova,Javascript,Android,Json,Html,Cordova,我制作了一个HTML5应用程序,可以从web服务访问JSON数据。当在桌面电脑上的浏览器中打开应用程序时,它可以正常工作,但当它被安卓应用程序“电话屏蔽”时,它就不工作了。我在PHP服务器上添加了以下行以启用CORS: header("Access-Control-Allow-Origin: *"); 代码如下: <!doctype html> <html lang="en"> <head> <meta charset="utf-8">

我制作了一个HTML5应用程序,可以从web服务访问JSON数据。当在桌面电脑上的浏览器中打开应用程序时,它可以正常工作,但当它被安卓应用程序“电话屏蔽”时,它就不工作了。我在PHP服务器上添加了以下行以启用CORS:

header("Access-Control-Allow-Origin: *");
代码如下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery.getJSON demo</title>
    <style>
        img {
            height: 100px;
            float: left;
        }
    </style>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
    <div id="images"></div>
    <script>
        function createCORSRequest(method, url) {
        var xhr = new XMLHttpRequest();
        if ("withCredentials" in xhr) {
            // XHR for Chrome/Firefox/Opera/Safari.
            xhr.open(method, url, true);
        } else if (typeof XDomainRequest != "undefined") {
            // XDomainRequest for IE.
            xhr = new XDomainRequest();
            xhr.open(method, url);
        } else {
            // CORS not supported.
            xhr = null;
        }
        return xhr;
    }
    function onMyClick() {
        console.info(new Object("Hello"));

        var url = 'http://example.com/mobile/index.php?tag=getAllById&catid=60';

        var xhr = createCORSRequest('GET', url);
        if (!xhr) {
            throw new Error('CORS not supported');
        }

        xhr.onload = function() {
            var responseText = xhr.responseText;
            console.info(responseText);
            // process the response.
        };

        xhr.onerror = function() {
             console.info('There was an error!');
        };
        xhr.send();
        console.info(new Object("Hello 4"));
    }
</script>
<input type="button" value="OK" onclick="onMyClick()"/>
</body>
</html>

jQuery.getJSON演示
img{
高度:100px;
浮动:左;
}
函数createCORSRequest(方法,url){
var xhr=new XMLHttpRequest();
如果(xhr中的“带凭证”){
//XHR适用于Chrome/Firefox/Opera/Safari。
open(方法、url、true);
}else if(XDomainRequest的类型!=“未定义”){
//XDomainRequest for IE。
xhr=新的XDomainRequest();
open(方法,url);
}否则{
//不支持CORS。
xhr=null;
}
返回xhr;
}
函数onMyClick(){
console.info(新对象(“Hello”);
var url='1〕http://example.com/mobile/index.php?tag=getAllById&catid=60';
var xhr=createCORSRequest('GET',url);
如果(!xhr){
抛出新错误(“不支持CORS”);
}
xhr.onload=函数(){
var responseText=xhr.responseText;
控制台信息(应答文本);
//处理响应。
};
xhr.onerror=函数(){
console.info('发生错误!');
};
xhr.send();
console.info(新对象(“hello4”);
}

不作为Android应用程序工作意味着我将在eclipse控制台中收到“有一个错误!”消息,因为OneError事件中的上述行将打印,而不是JSON数据作为响应文本。请任何人也帮我把它作为Android应用程序使用。

通过上面的例子,我看不出有什么不对,但有多种可能性。 您正在访问的域在config.xml中可能没有白名单?要允许一切,请包括以下内容:

我想到的另一件事是,你的应用程序需要互联网连接才能工作,你是否在AndroidManifest.xml中设置了所需的权限?
您需要查找
声明。

internet连接出现问题。事实上,我有两个互联网连接,因此我测试的平板电脑连接到了不工作的互联网连接,但我测试HTML5应用程序的PC连接到了工作的互联网连接&因此它作为HTML5应用程序工作,而不是作为“电话间隙”Android应用程序。