我正在处理此处给出的示例代码...
我正在使用 Android SDK 4.0.3 cordova-2.7.0.js jquery-1.9.0.min.js jquery.mobile-1.3.1.min.js auth.js
我正在尝试显示登录失败和登录成功警报对话框,但它不起作用..请告诉我我在哪里做错了......
MainActivity.java
package com.example.phonegaplogintest;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends DroidGap {
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/main.html");
this.appView.getSettings().setJavaScriptEnabled (true);
this.appView.addJavascriptInterface(new Console(), "console");
}
public class Console {
public static final String TAG = "Gap login test" ;
public void log ( String msg ) {
Log.d(TAG, msg) ;
}
}
}
索引.html
<!DOCTYPE html>
<html>
<head>
<title>GISPLUS</title>
<link rel="stylesheet" type="type/css" href="jquery.mobile-1.2.0.min.css"></link>
<!-- <script>var dojoConfig = { parseOnLoad: true };</script> -->
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery-1.9.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="auth.js"></script>
<!-- User-generated css -->
<style>
</style>
<!-- User-generated js -->
</head>
<body onload="init()">
<div id="launcherPage" data-role="page">
<!-- I'm just here waiting for deviceReady -->
</div>
<div id="loginPage" data-role="page">
<div data-role="header">
<h1>Auth Demo</h1>
</div>
<div data-role="content">
<form id="loginForm">
<div data-role="fieldcontain" class="ui-hide-label">
<label for="username">Username:</label> <input type="text"
name="username" id="username" value="" placeholder="Username" />
</div>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="password">Password:</label> <input type="password"
name="password" id="password" value="" placeholder="Password" />
</div>
<input type="submit" value="Login" id="submitButton">
</form>
</div>
</div>
<!-- <script>
$("#loginPage").live("pageinit", function(e) {
checkPreAuth();
});
</script> -->
</body>
</html>
main.html
<!doctype html>
<html>
<head>
<title>tittle</title>
<script>
window.location='./index.html';
</script>
<body>
</body>
</html>
auth.js
function init() {
document.addEventListener("deviceready", deviceReady, true);
delete init;
}
function checkPreAuth() {
console.log("checkPreAuth");
var form = $("#loginForm");
if(window.localStorage["username"] != undefined && window.localStorage["password"] != undefined) {
$("#username", form).val(window.localStorage["username"]);
$("#password", form).val(window.localStorage["password"]);
handleLogin();
}
}
function handleLogin() {
var form = $("#loginForm");
//disable the button so we can't resubmit while we wait
$("#submitButton",form).attr("disabled","disabled");
var u = $("#username", form).val();
var p = $("#password", form).val();
if(u != '' && p!= '') {
console.log("Submiting into URL...");
$.post("http://www.coldfusionjedi.com/demos/2011/nov/10/service.cfc?method=login&returnformat=json", {username:u,password:p}, function(res) {
if(res == true) {
console.log("Login Success.");
//store
window.localStorage["username"] = u;
window.localStorage["password"] = p;
$.mobile.changePage("some.html");
} else {
console.log("Login Failed.");
navigator.notification.alert("Your login failed", function() {});
}
$("#submitButton").removeAttr("disabled");
},"json");
} else {
console.log("Please enter u & p");
navigator.notification.alert("You must enter a username and password", alertDismissed, 'Login Failed!', 'OK' });
$("#submitButton").removeAttr("disabled");
}
return false;
}
function deviceReady() {
console.log("deviceReady");
$("#loginPage").on("pageinit",function() {
console.log("pageinit run");
$("#loginForm").on("submit",handleLogin);
checkPreAuth();
});
$.mobile.changePage("#loginPage");
}
function alertDismissed() {
// do something
}