1

我正在我的代码中尝试 Android 应用程序的 PhoneGap 插件。我的 HTML Strict 4 代码如下

代码:

<!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(),但它是未定义的。(我确实注意到并看到了)我观察到的是,当我<form /> 从 HTML 代码中删除元素时,它似乎起作用了。

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

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 问这种类型的问题。

4

1 回答 1

-1

您根本没有遵循插件规范,而且我没有看到您为 deviceready 设置侦听器的任何地方。

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

于 2012-07-11T12:09:10.097 回答