使用WebView加载URL后,将数据插入表单

时间:2015-04-07 10:04:45

标签: android webview

用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>

1 个答案:

答案 0 :(得分:0)

首先将您的数据导入所有Strings并将其转发到您想要的类:

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);");
        }