Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "The connection to the server was unsuccessful." in Cordova and jQuery

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:

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

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>

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");

                }
            });
});

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);
    }
}

ps. this is my first time using Cordova and JQuery

like image 430
khunjuice Avatar asked Feb 10 '23 20:02

khunjuice


2 Answers

This Error occurs when the webpage doesnot load / not reachable.

To fix this, create a new html file

main.html

<!doctype html>
<html>
  <head>
   <title>tittle</title>
   <script>
     window.location='./index.html';
   </script>
  <body>
  </body>
</html>

and keep the launch URL as main.html in config.xml

// Set by <content src="main.html" /> in config.xml


    loadUrl(launchUrl);

This will fix

like image 51
Mohammed Imran N Avatar answered Feb 15 '23 10:02

Mohammed Imran N


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" />

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.

like image 36
Puma Avatar answered Feb 15 '23 10:02

Puma