How to retrieve data from an open HTML form in a WebView?

1

Hello! I have a responsive web application that I intend to open via android application through a WebView. Well, the only problem is that I would need to grab some data from the submit from only one application form to send to a bluetooth printer (whose code is java based). Is there a way to get this data that comes from the web application and pass it to my java application? The simpler the better. Thanks in advance.

    
asked by anonymous 15.09.2016 / 18:13

1 answer

2

You need to add an interface, at the time the webView declares.

Example:

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");

WebAppInterface class code:

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

Now in your PHP, you should include the onClick in your input.

Example:

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

And the JavaScript that will be responsible for "chatting" with Android:

Example:

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

You can see more examples and details on the Android documentation

    
15.09.2016 / 19:03