Pass a value to loadURL - Android

2020-02-25 23:06发布

问题:

Is it possible to pass a value to the URL mentioned in webView.loadUrl? something like this??

webView.loadUrl("file:///android_asset/www/index.html#value="+value);

or is there any way to pass a Java String value to the Javascript function in loadURL?

回答1:

I found the solution.. posting it here for the sake of others :)

I added the following snippet of code in my Activity class which solved the problem,

webView.setWebViewClient(new WebViewClient() {  
                @Override  
                public void onPageFinished(WebView view, String url)  
                {  
                    webView.loadUrl("javascript:callMe(\""+data_val+"\")");

                }  
            });  

Thanks all :)



回答2:

You'll want to use a GET-Query string to do so. Note that the maximum length of a URL is 256 chars!

After you supplied your arguments that way, you can use JavaScript to read them (by cutting them out of the URL). Here is a Code-Snipped and an article on this topic.



回答3:

I found a simple solution. Below is the code which is working

String s = "http://10.0.2.2/myhtml/add.php?bc=" + bc;   
myWebView.loadUrl(s);