I can show up HTML file content in android webview well.Now how could i pass parameter into HTML file.For ex.my HTML content has an video player i need to pass dynamic values(URL) into HTML file for playing dynamic video.My HTML file is located on asset folder.How could i do this?
Thanks.
Instead of passing directly the video URL (following you example), i would have used tokens in the Html file. For example:
where the
$VIDEO_URL$
will be the token wich will be replaced during the runtime with a real videoURL
.Also, since you cannot change the contents of your asset folder during runtime you should load the html file contents into a String variable and use the
replace
method to replace the token with a realURL
and, finally, pass that string to your webview. Something like this:I managed to pass variables in a different way.
My problem was that everytime I switched to another app, when coming to the webapp, the webview kept reloading. I guess that's because of the following line in my
onCreate()
method:myWebView.loadUrl(url);
I had the idea to pass these state variables in the url, but as you know it is not possible yet. What I did was to save the state of some variables usingonSaveInstanceState(Bundle outState) {...}
and restore them withonRestoreInstanceState(Bundle savedInstanceState){...}
.In onCreate method after setting up myWebView I did the following:
So, what it happens is that first I load the page with the default URL (www.yoururl.com) and onPageFinished I call a new javascript method where I pass the variables. In javascript
loadVariables
function looks like this:here assets means what?
String template = Utils.inputStreamToString(assets.open("html/template.html"));
I came upon this problem today, however I needed this to work with UTF-8 encoding, so this was my approach, hopefully it will help someone and clarify some of the previous answers to this question.
HTML:
Java:
As for IOUtils: http://commons.apache.org/proper/commons-io/download_io.cgi
If i would like to have something dynamic in my HTML i would have an html with dynamic parts written like this:
Then i would load my HTML:
then Then i would replace all dynamics parts with what i want like this:
then i would pass it to my webView!