Android 使用WebView加载url后将数据插入表单

Android 使用WebView加载url后将数据插入表单,android,webview,Android,Webview,在我用WebView加载url后,我可以在表单页面中自动插入一些数据吗 我尝试: @Override public void onPageFinished(WebView view, String url) { String user="pippo"; String pwd="secret"; view.loadUrl("javascript:document.getElem

在我用WebView加载url后,我可以在表单页面中自动插入一些数据吗

我尝试:

 @Override
            public void onPageFinished(WebView view, String url) {
                String user="pippo";
                String pwd="secret";
                view.loadUrl("javascript:document.getElementById('j_username').value = '"+user+"';document.getElementById('j_password').value='"+pwd+"';");
            }
但不是工作

HTML源代码是:

<html>

....
    <form action="/.../" method="post">

                <table align="center">
                <tr>
                <td>
                <table>
                <tr>
                <td>User:</td>
                <td><input name="j_username" type="text" tabindex="1"></td>
                </tr>
                <tr>
                <td>Password:</td>
                <td><input name="j_password" type="password" tabindex="2"></td>
                </tr>
                <tr>
                <td colspan="2"><input type="submit" value="Login" tabindex="3"></td>
                </tr>
                </table>
                </td>
                </tr>
                </table>
                        <!--</form>-->
                        </form>

...
<\html>

....
用户:
密码:
...

首先将数据放入所有
字符串中
,然后将它们转发到您想要的类中,如下所示:

class MyJavaScriptInterface {

    private Context ctx;

    MyJavaScriptInterface(Context ctx) {
        this.ctx = ctx;
    }
@JavascriptInterface
    public void showHTML(String html) throws JSONException {
        System.out.println(html);
        JSONObject jsonResponse = new JSONObject(html);
        String sts = jsonResponse.getString("status");
        if (sts.equals("success")) {
            message = jsonResponse.getString("msg");
            approved_status = jsonResponse.getString("approved");
            jsonObj = jsonResponse.getJSONObject("data");
            user_email = jsonObj.getString("user_email");
            phone_no = jsonObj.getString("phone_no");
            user_gender = jsonObj.getString("gender");
            first_name = jsonObj.getString("first_name");
            last_name = jsonObj.getString("last_name");
            ids = jsonObj.getString("id");
            id = Integer.parseInt(ids);
}
}
如果您的最后一页位于
HTML
中,请使用以下代码:

@Override
        public void onPageFinished(WebView view, String url) {

            wb.loadUrl("javascript:window.HtmlViewer.showHTML"
                    + "(document.getElementsByTagName('pre')[0].innerHTML);");
        }

在这两个表单中,您在哪里指定id j_用户名?我只看到名称、类型和tabindex属性..ops yes..我必须使用document.getElementByName?getElementByName()不存在,您必须使用getElementsByName返回集合,并且您必须获得位置[0]的元素作为用户名,位置[1]的元素作为密码。只有当这两个名称属性是您加载的html页面中的前两个时,这才有效。如果您可以更改html,我强烈建议您使用id属性,它们更精确,并且在添加html代码时不会更改。@Fondesa因此,如果我尝试使用:javascript:document.getElementsByTagName('INPUT')[0]。值=“+user+”;document.getElementsByTagName('INPUT')[1]。值='“+pwd+”;“只有这个表单有效吗?它是从哪里获取数据的HTML页面吗?”?