Popular Posts
Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... SwiXml - Layout BorderLayout BorderLayoutPane.xml <?xml version="1.0" encoding="UTF-8"?> <panel layout="BorderLayout...
Stats
Operate form in WebView and get content
class ContentLoader {
    @JavascriptInterface
    public void load(String body) {
        // do something
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView webView = createWebView();

    webView.loadUrl("https://www.somedoamin.com/product.jsp");
    // enable javascript
    webView.getSettings().setJavaScriptEnabled(true);
    // add interface
    webView.addJavascriptInterface(new ContentLoader(), "ContentLoader");
    webView.setWebViewClient(new WebViewClient() {

        int action;

        @Override
        public void onPageFinished(WebView view, String url) {
            switch (action) {
                case 0:
                    // fill field and submit
                    view.loadUrl("javascript:{\n" +
                            "\tdocument.querySelector('input[name=cgi_tel_no]').value='12345678';\n" +
                            "\tdocument.querySelector('input[name=cgi_id_no]').value='abcdef';\n" +
                            "\tsubmitMyForm();\n" +
                            "};");
                case 1:
                    // get content from 'ContentLoader' interface
                    view.loadUrl("javascript:{\n" +
                            "\twindow.ContentLoader.load('<body>'+document.body.innerHTML+'</body>');\t\n" +
                            "};");
                    break;
            }

            action++;
        }
    });
}

WebView createWebView() {
    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 0;
    params.width = 0;
    params.height = 0;

    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

    WebView view = new WebView(this);
    view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    layout.addView(view);

    windowManager.addView(layout, params);

    return view;
}