简体   繁体   English

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

[英]Phonegapped Android application not working as compared to HTML5 app

I made a HTML5 app, which accessess JSON data from a web service. 我制作了一个HTML5应用,该应用可以从Web服务访问JSON数据。 The app works fine when opened in a browser on desktop computer, but when it is "phonegapped" into Android application it is not working. 在桌面计算机上的浏览器中打开该应用程序后,它可以正常工作,但是当将其“插入电话”到Android应用程序中时,该应用程序将无法正常工作。 I have added following line on the PHP server to enable CORS: 我在PHP服务器上添加了以下代码来启用CORS:

header("Access-Control-Allow-Origin: *");

Following is the code: 以下是代码:

<!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>

Not working as Android app means that I am getting, "There was an error!" 无法使用Android应用程序表示我收到“出现错误!” message as the above line in onerror event prints, instead of the JSON data as response text, in the console of eclipse. 在eclipse的控制台中,显示onerror事件中上一行的message消息,而不是JSON数据作为响应文本。 Please could anyone help me get this working as Android application also. 请有人能帮助我也将其作为Android应用程序使用。

With the above example I can't see what is wrong but there are multiple possibilities. 通过上面的示例,我看不到出什么问题了,但是有多种可能。 The domain you are accessing might not be white listed in your config.xml? 您正在访问的域可能未在config.xml中列出为白色? To allow everything include the following: <access origin="*" /> 为了使所有内容包括以下内容: <access origin="*" />

See docs about white list for more info here 在此处查看有关白名单的文档以获取更多信息

Another thing which comes to mind, your app needs internet connection to work, do you have the required permissions setup in AndroidManifest.xml? 想到的另一件事是,您的应用需要互联网连接才能工作,您是否在AndroidManifest.xml中设置了必需的权限? You need to find the <uses-permission android:name="android.permission.INTERNET" /> declaration. 您需要找到<uses-permission android:name="android.permission.INTERNET" />声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM