android – Passing data from java class to Web View html-ThrowExceptions

Exception or error:

I’m loading below html in my webView

Now what I want to do is to fill the textbox in the html that came from my java class variable and then automatically hit submit.

But I don’t have any idea how to do this.

Any thougths will be appreciated.

How to solve:

First, your URL seems not available.

If you want to do data exchange between android app and your web app/web page you can achieve this via javascript.

Here is an example from Android official site:

Create a class like this:

public class JavaScriptInterface {
    Context mContext;

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

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

In your WebView:

WebView webView = (WebView) findViewById(;
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

In your web page:

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

<script type="text/javascript">
    function showAndroidToast(toast) {

If you wanna pass something to your webpage, just calling corresponding javascript function:

String str = "xxx";

Here is the Reference:


I would add that the load of the javascript function should be done when the html is loaded. To control that, you can use the following:

webview.setWebViewClient(new WebViewClient(){
    public void onPageFinished(WebView view, String url){   
        webview.loadUrl("javascript:init('" + theArgumentYouWantToPass + "')");


<!DOCTYPE html>


<script type="text/javascript">

    function init(val){
// Do whatever you want with your parameter val

Taken from Uncaught ReferenceError: myFunction is not defined at null:1 Android exception in webview


Be careful to call javascript function like this, the str may include single quote or other special characters.

String str = "xxx";

I suggest to encode the str in base64, and decode it on javascript side.

  • Android

    String str = "xxx";
    //encode in base64
    String base64Str = Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);
    myWebView.loadUrl("javascript:xxx('"+ base64Str +"')");
  • Javascript

    function xxx(val) {
        //decode from base64
        var str = atob(data)


Just enable DOM Storage and write var x= to string:


webview.setWebViewClient(new WebViewClient(){

public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    String js = "javascript:var x =document.getElementById('username').value = '"+user+"';var y=document.getElementById('password').value='"+pass+"';";

    if (Build.VERSION.SDK_INT >= 19) {
        view.evaluateJavascript(js, new ValueCallback<String>() {
            public void onReceiveValue(String s) {
    else {


Pass the paramter directly in the url


Get the paramter in html file

var url_string = window.location.href
var url = new URL(url_string);
var message= url.searchParams.get("message");

Leave a Reply

Your email address will not be published. Required fields are marked *