错误“到服务器的连接不成功”。在Cordova和jQuery

问题描述:

我使用Cordova和JQuery mobile为android创建了一个应用。它运行良好,当我测试运行代码使用google chrome,但是当我尝试运行它在android模拟器使用android studio在 cmd locate> cordova emulate android )它不工作。

I created an app for android using Cordova and JQuery mobile. It works well when I test run the code using google chrome, but when I try to run it on android emulator using android studio in cmd (locate>cordova emulate android) it doesn't work.

当我尝试在模拟器上运行它时,我得到:

When I try to run it on the emulator I get:


与服务器的连接失败(file:///android_asset/www/index.html)

"The connection to the server was unsuccessful. (file:///android_asset/www/index.html)"

但如果我不使用JQuery,它工作正常。我没有在JQuery或JQuery mobile中修改任何代码。

But if I don't use JQuery, it works fine. I didn't modify any code in JQuery or JQuery mobile.

    <head>
        <-- import jquery and codovar -->
        <link rel="stylesheet" href="./css/jquery.mobile-1.4.5.min.css" />

        <-- if i remove this 3 line of code below program is working as same as open with google chrome -->
        <link rel="stylesheet" href="./css/jquery.mobile-1.4.5.min.css" />
        <script src="./js/jquery-2.1.4.min.js"></script>
        <script src="./js/jquery.mobile-1.4.5.min.js"></script>
        <--end remove here -->

        <script type="text/javascript" src="./js/Login.js"></script>

    </head> 
    <body>
        <div data-role="page" id="page1">
            <div data-role ="header" style="background-color: #00CCA5;">
                <h1 style="color: #FAF0BA; text-shadow: None; text-align: center;">LogIn</h1>
            </div>
            <div data-role ="main" class="ui-content"  >
                <div align="center">
                    <label>id:</label>
                    <input type="text" id="login_id">
                    <br>
                    <label>password:</label>
                    <input type="password" id="login_password">
                    <div id="wrong" style="color: red;"><br></div>
                </div>
                <button style="background-color: #FAF0BA; color:#00CCA5;" data-role ="button" data-icon ="forward" id="let_login">Log in</button>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
    </body>
</html>

这里是 login.js p>

and here is login.js

$(document).ready(function () {
    $("#let_login").on("tap",
            function () {
                var id = $("#login_id").val();
                var password = $("#login_password").val();
                if (id == "test" && password == "password") {
                    document.location.href = "./customer.html";
                    //$.mobile.changePage("./customer.html");
                }
                else {
                    $("#wrong").html("Wrong id and/or password");

                }
            });
});

这里是 MainActivity.java 文件读取一些旧帖,但仍然不工作)

and here is MainActivity.java (modified file after read some old post but still doesn't work)

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //modify line below
        super.setIntegerProperty("loadUrlTimeoutValue", 70000);
        //end modify line 

        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }
}

这是我第一次使用Cordova和JQuery

ps. this is my first time using Cordova and JQuery

我建议先检查你的config.xml有一个部分可以指定应用程序的起始页。它必须是pontting到index.html,我认为你的页面被称为Login.html
该部分看起来像这样

I recommend first, to check your config.xml there is a section where you can specify the start page of your app. It must be ponting to "index.html" and I think your page is called "Login.html" (Just a guess) The section looks like this

<content src="your_page.html" />

只需更改该部分,然后尝试。
如果这个部分设置正确,那么Mohhamed Imran N的方法工作,我已经测试过了。
希望这有帮助。

just change that section and try. If this section is set correctly, then the approach by Mohhamed Imran N works, I have tested it. Hope this helps.