2012-07-24 30 views
7

我可以顯示在android webview中的HTML文件內容well.Now我怎樣才能將參數傳遞到HTML文件。對於ex.my HTML內容有一個視頻播放器 我需要通過動態值(URL)轉換爲HTML文件以播放動態視頻。我的HTML文件位於資產文件夾中。我該如何操作?如何將參數傳遞到HTML文件從android

謝謝。

+0

http://stackoverflow.com/q/11614456/1012284 – 2012-07-24 08:32:07

回答

4

而不是直接傳遞的視頻網址(以下例子),我會在HTML文件中使用令牌。例如:

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

其中$VIDEO_URL$將令牌至極將與真正的視頻URL在運行時進行更換。

而且,因爲你不能在運行過程中改變了資產的文件夾的內容,你應該在html文件的內容加載到變量中的字符串,並使用replace方法與真正的URL更換令牌,並最終傳遞字符串你的webview。類似這樣的:

//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"); 
+0

VAR的flashvars = {\t SRC: 「我SORCE URL」 \t \t \t \t \t \t,\t將autoPlay:真 \t \t \t,\t controlBarAutoHide:假 \t \t \t \t};這是我的HTML在我的src行。我可以替換這個「html = html.replace(」src「,real_video_url); 」 – sanjay 2012-07-24 08:50:32

+0

在你的情況,如果我得到這個正確的,你需要使用'html = html。替換(「My Sorce URL」,real_video_url);'而不是'html = html.replace(「src」,real_video_url);'。只要記住沒有任何其他的令牌「My Sorce URL」的序列,因爲'replace'方法會將它們全部重新放在您的html中,並使用'real_video_url'變量的內容。 – Angelo 2012-07-24 08:58:27

+0

我試過你的方式。但是當我運行時,它顯示黑屏(沒有視頻播放)? – sanjay 2012-07-24 09:15:14

3

如果我想有東西在我的HTML動態我將不得不這樣寫的動態部分的HTML:

<B>%NAME%</B> 

話,我會加載我的HTML:

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

然後 然後我會用我想要的所有動態部分替換這樣的:

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

然後我會將它傳遞給我的webView!

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

無法導入utils的課嗎?你能告訴我在哪裏可以得到這個? – Google 2016-03-10 05:16:25

1

這裏的資產意味着什麼?

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

+0

context.getAssets() – 2017-01-11 05:35:46

7

我今天遇到了這個問題,但是我需要這個與UTF-8編碼一起工作,所以這是我的方法,希望它能幫助某人並澄清一些此前問題的答案。

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

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

2

我設法傳遞變量以不同的方式。

我的問題是,每次我切換到另一個應用程序,當來到webapp,webview不斷重新加載。我想這是因爲我的onCreate()方法中的以下行:myWebView.loadUrl(url);我的想法是在URL中傳遞這些狀態變量,但如您所知,這是不可能的。 我所做的就是使用onSaveInstanceState(Bundle outState) {...}保存一些變量的狀態,並使用onRestoreInstanceState(Bundle savedInstanceState){...}恢復它們。

在onCreate方法建立myWebView後,我做了以下內容:

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

那麼,什麼情況是,我第一次加載使用默認網址的頁面(www.yoururl.com)和onPageFinished我打電話一個新的JavaScript方法,我通過變量。 在javascript中loadVariables功能如下:

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 
    } 
} 
相關問題