PhoneGap PlugIn无法正常工作

时间:2012-07-11 07:40:02

标签: android html cordova phonegap-plugins

我在我的代码中尝试使用Android应用的PhoneGap插件。我的HTML严格4代码如下

CODE:

<!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;

function validate(){    

    //uname = document.forms[0].elements[0].value;
    //var pass = document.forms[0].elements[1].value;

    uname=document.getElementById("i1").value;
    var pass=document.getElementById("i2").value;

    alert("Uname: "+uname+"\r\nPass: "+pass);

    if(!uname || uname === "" || !pass || pass === ""){

        alert("User Credentials are incorrect");

    }
    else{           

        //Make a webservice call
          post_data(uname,pass,postDataCB);     


    }

}

function postDataCB(retval){

    alert("In postDataCB()\r\nuname: "+uname);

}



</script>
</head>
<body>
   <form>
      User name: <input type="text" id="i1" name="username" value="GEO02-OTPUAT" /><br />
      Password:&nbsp;&nbsp;<input type="password" id="i2" name="pwd" value="aaa111" /><br />
      <button onclick="javascript:validate()">Submit</button><br />
  </form>

在我的HTML中,我有一个名为uname的全局变量。此变量在回调函数postDataCB()中使用,但它是未定义的。 (我确实警觉并且看到了) 我观察到的是当我从HTML代码中删除<form />元素时,它似乎有效。

任何人都可以告诉我它为什么会发生以及如何解决这个问题。

post_data的代码:

public PluginResult post_data(JSONArray funcargs, String jscallbackid){

    SuccessCallBack=funcargs.getString(0);
    FailureCallBack=funcargs.getString(1);
        uname= funcargs.getString(2);
    passw = funcargs.getString(3);      


    conn = new URL("http://www.subratlogin.com/login").openConnection();
    conn.setDoOutput(true);


    data += URLEncoder.encode(uname, "UTF-8") + "=" + URLEncoder.encode(passw, "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(); 

    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;    
 }
sry fr问这类问题。

1 个答案:

答案 0 :(得分:-1)

您根本没有关注插件规范,我没有看到您为设备准备设置侦听器的任何地方。

http://wiki.phonegap.com/w/page/36753494/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20Android