How to pass parameter to HTML file from android

I can show the contents of the HTML file in the Android browser well. Now how can I pass the parameter to an HTML file. There is a video player for ex.my HTML content. I need to transfer dynamic values ​​(URLs) to an HTML file to play dynamic video. My HTML file is in the resource folder. How can i do this?

Thanks.

+7
source share
5 answers

Instead of directly passing the video URL (after your example), I would use tokens in the Html file. For example:

<embed src="$VIDEO_URL$" autostart="false" /> 

where $VIDEO_URL$ will be the token that will be replaced at runtime with the real video URL .

In addition, since you cannot change the contents of your resource folder at run time, you must load the contents of the html file into a String variable and use the replace method to replace the token with a real URL and finally pass that line into your webview. Something like that:

 //The html variable has the html contents of the file stored in the assets folder //and real_video_url string variable has the correct video url html = html.replace("$VIDEO_URL$", real_video_url); webview.loadData(html, "text/html", "utf-8"); 
+4
source

Today I ran into this problem, but I need it to work with UTF-8 encoding, so this was my approach, I hope this helps someone and clarifies some of the previous answers to this question.

HTML:

 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <h1>%ERR_TITLE%</h1> <h2>%ERR_DESC%</h2> </body> </html> 

Java:

 String content = IOUtils.toString(getAssets().open("error.html")) .replaceAll("%ERR_TITLE%", getString(R.string.error_title)) .replaceAll("%ERR_DESC%", getString(R.string.error_desc)) mWebView.loadDataWithBaseURL("file:///android_asset/error.html", content, "text/html", "UTF-8", null); 

Regarding IOUtils: http://commons.apache.org/proper/commons-io/download_io.cgi

+7
source

If I wanted to have something dynamic in my HTML, I would have html with dynamic parts written as follows:

 <B>%NAME%</B> 

Then I would upload my HTML:

 String template = Utils.inputStreamToString(assets.open("html/template.html")); 

then I would replace all parts of the dynamics with what I want:

 String data = template.replaceAll("%NAME%", "Alice McGee"); 

then I will pass it to my webView!

 WebView webView = new WebView(this); webView.loadDataWithBaseURL("file:///android_asset/html/", data, "text/html", "utf-8", null); 
+3
source

I managed to pass the variables differently.

My problem was that every time I switched to another application, when I came to webapp, the webview kept reloading. I assume that due to the following line in my onCreate() method: myWebView.loadUrl(url); I had the idea of ​​passing these state variables to a URL, but as you know, this is not yet possible. I did to save the state of some variables using onSaveInstanceState(Bundle outState) {...} and restore them using onRestoreInstanceState(Bundle savedInstanceState){...} .

In the onCreate method, after setting up myWebView, I did the following:

 myWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String urlString) { Log.i("onPageFinished", "loadVariables("+newURL+")"); if(newURL!="") myWebView.loadUrl("javascript:loadVariables("+"\""+newURL+"\")"); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); jsInterface = new JSInterface(this,myWebView); myWebView.addJavascriptInterface(jsInterface, "Android"); if (savedInstanceState != null) { // retrieve saved variables and build a new URL newURL = "www.yoururl.com"; newURL +="?var1=" + savedInstanceState.getInt("key1"); newURL +="?var2=" + savedInstanceState.getInt("key2"); Log.i("myWebApp","NEW URL = " + newURL); } myWebView.loadUrl("www.yoururl.com"); 

So what happens is that first I load the page with the default URL (www.yoururl.com) and onPageFinished. I am calling a new javascript method where I pass in the variables. In the javascript function, loadVariables looks like this:

 function loadVariables(urlString){ // if it is not the default URL if(urlString!="www.yoururl.com") { console.log("loadVariables: " + urlString); // parse the URL using a javascript url parser (here I use purl.js) var source = $.url(urlString).attr('source'); var query = $.url(urlString).attr('query'); console.log("URL SOURCE = "+source + " URL QUERY = "+query); //do something with the variables } } 
+2
source

here assets mean what?

String template = Utils.inputStreamToString (assets.open ("html / template.html"));

+1
source

All Articles