我已经为v1.0.0实现了Phonegap插件。 请查看我的HTML代码
/**** HTML FILE ****/
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum- scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8" src="system.js"></script>
<script type="text/javascript">
var uname = "User1"; //------------- 2
var System = function() {};
System.prototype.func = function(funcname,funcdata) {
//alert("In the list function");
return PhoneGap.exec(
null, //Success callback from the plugin
null, //Error callback from the plugin
'SystemPlugin', //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
funcname, //Tell plugin, which action we want to perform
funcdata); //Passing list of args to the plugin
};
PhoneGap.addConstructor(
function() {
PhoneGap.addPlugin("system", new System());
}
);
var _anomFunkMap = {};
var _anomFunkMapNextId = 0;
function anomToNameFunk(fun)
{
var funkId = "f" + _anomFunkMapNextId++;
var funk = function()
{
fun.apply(this,arguments);
_anomFunkMap[funkId] = null;
delete _anomFunkMap[funkId];
}
_anomFunkMap[funkId] = funk;
return "_anomFunkMap." + funkId;
}
function GetFunctionName(fn)
{
if (typeof fn === "function") {
var name= fn.name;
if (!name) {
var m = fn.toString().match(/^\s*function\s+([^\s\(]+)/);
name= m && m[1];
}
if (name && (window[name] === fn)) {
return name;
} else {
return anomToNameFunk(fn);
}
}else {
return null;
}
}
function post_sync_data(url, urlparam, param, requestheader, callback){
//alert("In post_sync_data");
if(undefined == callback){
if(undefined == requestheader){
var fnname = GetFunctionName(param);
var dataArray = [fnname, fnname, url, urlparam];
}
else{
var fnname = GetFunctionName(requestheader);
//Note : the param is optional parameter or request header
var dataArray = [fnname, fnname, url, urlparam, param];
}
}
else{
var fnname = GetFunctionName(callback);
var dataArray = [fnname, fnname, url, urlparam, param, requestheader];
}
//alert("Calling plugin function post with \r\n"+dataArray);
var r = window.plugins.system.func("post",dataArray);
//alert("r is :\r\n"+r); ------------- 3
return r;
}
function validate(){
//Make a webservice call for the Omnesys Token
post_sync_data(url,urlparam,uname,postDataCB);
}
function postDataCB(token,name){
//Here uname is undefined.
alert("In postDataCB()\r\nuname: "+uname+"\r\nuname from C Func: "+name+"\r\n\r\ntoken: "+token);
return;
}
function onBodyLoad()
{
document.addEventListener("deviceready",onDeviceReady,false);
}
function onDeviceReady()
{
// do your thing!
navigator.notification.alert("PhoneGap is working")
document.getElementById('d1').style.visibility = "visible";
}
</script>
</head>
<body onload="onBodyLoad()">
<div id="d1" style="visibility:hidden">
<form> <!-- 1 -->
<button onclick="javascript:validate()">Submit</button><br />
</form>
</div>
</body>
</html>
我的Phonegap插件文件。
/*** My PhoneGap Plugin.xml File ***/
<?xml version="1.0" encoding="utf-8"?>
<plugins>
<plugin name="App" value="com.phonegap.App"/>
<plugin name="Geolocation" value="com.phonegap.GeoBroker"/>
<plugin name="Device" value="com.phonegap.Device"/>
<plugin name="Accelerometer" value="com.phonegap.AccelListener"/>
<plugin name="Compass" value="com.phonegap.CompassListener"/>
<plugin name="Media" value="com.phonegap.AudioHandler"/>
<plugin name="Camera" value="com.phonegap.CameraLauncher"/>
<plugin name="Contacts" value="com.phonegap.ContactManager"/>
<plugin name="Crypto" value="com.phonegap.CryptoHandler"/>
<plugin name="File" value="com.phonegap.FileUtils"/>
<plugin name="Network Status" value="com.phonegap.NetworkManager"/>
<plugin name="Notification" value="com.phonegap.Notification"/>
<plugin name="Storage" value="com.phonegap.Storage"/>
<plugin name="Temperature" value="com.phonegap.TempListener"/>
<plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>
<plugin name="Capture" value="com.phonegap.Capture"/>
<plugin name="SystemPlugin" value="com.phonegap.test.SystemPlugin" />
</plugin>
我的JAVA班
/** Plugin Code **/
public class SystemPlugin extends Plugin {
@Override
public PluginResult execute(String funcname, JSONArray funcargs, String jscallbackid){
//Get the success and failure call back functions
try{
SuccessCallBack = funcargs.getString(0);
FailureCallBack = funcargs.getString(1);
}
catch (JSONException jsonEx) {
return null;
}
if(funcname.equals("post")){
try{
url = funcargs.getString(2);
urlparam = funcargs.getString(3);
}
catch (JSONException e) {
SendJS = "javascript:" + FailureCallBack + "('" + e.getMessage() + "')";
sendJavascript(SendJS);
return null;
}
try {
conn = new URL(url).openConnection();
conn.setDoOutput(true);
} catch (MalformedURLException e) {
SendJS = "javascript:" + FailureCallBack + "('" + e.getMessage() + "')";
sendJavascript(SendJS);
return null;
} catch (IOException e) {
SendJS = "javascript:" + FailureCallBack + "('" + e.getMessage() + "')";
sendJavascript(SendJS);
return null;
}
if(requestheader == null){
//Default Request Property
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
}
else{
String Headers[] = requestheader.split("\r\n");
for(i=0; i < Headers.length; i++){
String hReq[] = Headers[i].split(":");
if(hReq.length == 2){
conn.setRequestProperty(hReq[0], hReq[1]);
}
}
}
try {
//Blank String
String data="";
String param[] = urlparam.split("&");
for(i=0; i < param.length; i++){
String keyval[] = param[i].split("=");
if(keyval.length == 2){
data += URLEncoder.encode(keyval[0], "UTF-8") + "=" + URLEncoder.encode(keyval[1], "UTF-8") + "&";
}
}
//remove the unwanted & at the end of the string
data = data.substring(0,data.length()-1);
ro = new OutputStreamWriter(conn.getOutputStream());
ro.write(data);
//Close the connection
ro.close();
} catch (IOException e) {
SendJS = "javascript:" + FailureCallBack + "('" + e.getMessage() + "')";
sendJavascript(SendJS);
return null;
}
try{
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null)
{
sb.append(line);
}
//Close the connection
rd.close();
} catch (IOException e) {
SendJS = "javascript:" + FailureCallBack + "('" + e.getMessage() + "')";
sendJavascript(SendJS);
return null;
}
SendJS = "javascript:" + SuccessCallBack + "('" + JSONObject.quote(sb.toString());
if(jObj != null)
SendJS += "','" + jObj + "')";
else if(StringParam != null)
SendJS += "','" + StringParam + "')";
else
SendJS += "')";
sendJavascript(SendJS);
return null;
}
else{
return null;
}
}
}
现在在第一个代码中你可以看到数字1 2 3 1 - 表单元素 2 - 是一个全局变量 3 - 是插件警报
现在,如果我删除1.一切正常。但如果我保留表格,全局变量就无法保持其价值。它变为undefined
,警报也会提供undefined
有人可以指导我这是什么问题吗?
答案 0 :(得分:0)
这是表单问题。表格不符合标准表格格式 纠正这个问题解决了这个问题。