7

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.

sanjay
  • 2,590
  • 17
  • 55
  • 88

5 Answers5

11

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:

<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);

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

Jonas Masalskis
  • 1,206
  • 1
  • 15
  • 14
4

Instead of passing directly the video URL (following you example), i would have used tokens in the Html file. For example:

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

where the $VIDEO_URL$ will be the token wich will be replaced during the runtime with a real video URL.

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 real URL and, finally, pass that string to your webview. Something like this:

//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");
AggelosK
  • 4,313
  • 2
  • 32
  • 37
  • var flashvars ={ src: "My Sorce URL" , autoPlay: true , controlBarAutoHide: false }; This is my src line on my HTML.Can i replace like this "html = html.replace("src", real_video_url); " – sanjay Jul 24 '12 at 08:50
  • In your case, if i got this correctly, you need to use `html = html.replace("My Sorce URL", real_video_url);` instead of `html = html.replace("src", real_video_url);`. Just keep in mind to not have any other sequences of the token "My Sorce URL" since the `replace` method will repace them all in your html with the contnts of the `real_video_url` variable. – AggelosK Jul 24 '12 at 08:58
  • Ive tried your way.But when i run.,it shows black screen(No video play)?? – sanjay Jul 24 '12 at 09:15
  • Maybe the problem is that you display a video in webview. Maybe this previous SO post can help: http://stackoverflow.com/questions/3815090/webview-and-html5-video. Also if you just need to show a video, you can consider using a VideoView instead. See for help here:http://stackoverflow.com/a/2659346/1482726 , just do not forget to add the required permision to your Manifest.xml – AggelosK Jul 24 '12 at 09:22
  • But my src URL is an rtmp not HTTP..So i think i can play rtmp video on webview – sanjay Jul 24 '12 at 09:27
  • I think this is why you have problems displaying the video. Googling i found this SO post:http://stackoverflow.com/a/10414373/1482726 It may come handy to you. – AggelosK Jul 24 '12 at 09:33
  • Nope.I dont need like "RTMP streaming in android".Just to show up HTML content & need to pass values from android – sanjay Jul 24 '12 at 09:46
  • To check if the problem is in my solution or the rmtp streaming, try using a valid html file in your asset folder and check if the video is displaying correctly (if you have not done that already). By valid html file, i mean an html file that has not any token for replacement, just a valid video url. – AggelosK Jul 24 '12 at 09:55
3

If i would like to have something dynamic in my HTML i would have an html with dynamic parts written like this:

<B>%NAME%</B>

Then i would load my HTML:

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

then Then i would replace all dynamics parts with what i want like this:

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

then i would pass it to my webView!

WebView webView = new WebView(this);
webView.loadDataWithBaseURL("file:///android_asset/html/", data, "text/html", "utf-8", null);
Alehar
  • 639
  • 5
  • 16
2

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 using onSaveInstanceState(Bundle outState) {...} and restore them with onRestoreInstanceState(Bundle savedInstanceState){...}.

In 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 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:

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 
    }
}
bboydflo
  • 907
  • 1
  • 15
  • 24
1

here assets means what?

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

merup shah
  • 86
  • 1
  • 4