2015-06-15 29 views
4

我使用Cordova和JQuery mobile創建了一個android應用程序。它運行良好,當我測試使用谷歌瀏覽器運行代碼,但是當我嘗試在Android模擬器上使用cmdlocate>cordova emulate android)中的android studio運行它時,它不起作用。錯誤「到服務器的連接不成功。」在Cordova和jQuery

當我嘗試在模擬器上運行它,我得到:

「到服務器的連接失敗(文件:///android_asset/www/index.html)」

但是,如果我不使用JQuery,它工作正常。我沒有修改JQuery或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

$(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(修改後的文件中讀取後一些老的文章,但仍然無法正常工作)

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。這是我第一次使用Cordova和JQuery

回答

1

我建議首先檢查你的config.xml文件,你可以在其中指定應用的起始頁面。必須龐廷爲「index.html的」,我認爲你的頁面被稱爲「login.html的」(只是猜測) 的部分看起來像這樣

<content src="your_page.html" />

只是改變部分並嘗試。 如果本節設置正確,那麼Mohhamed Imran N的方法有效,我測試過了。 希望這有助於。

7

當網頁沒有加載/不可達時發生此錯誤。

爲了解決這個問題,創建一個新的HTML文件

main.html中

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

,保持啓動URL爲main.html中 config.xml中

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


    loadUrl(launchUrl); 

這將修復

+0

我只有cordova和插件注入main.html而不是index.html – viskin

0

我已經使用了以下內容:

<content src="./index.html" /> 

的頁面地址之前,只需添加./和它的作品!

相關問題