2012-07-24 7 views
7

HTML 파일 콘텐츠를 Android 웹보기 well에 표시 할 수 있습니다. 어떻게 HTML 파일에 매개 변수를 전달할 수 있습니까? ex.my HTML 콘텐츠에 비디오 플레이어가 있습니다. 동적으로 전달해야합니다. 값 (URL)을 동적 파일 재생을위한 HTML 파일로 변환합니다. 내 HTML 파일은 자산 폴더에 있습니다. 어떻게해야합니까?android에서 HTML 파일로 매개 변수를 전달하는 방법

감사합니다.

+0

http://stackoverflow.com/q/11614456/1012284 –

답변

4

예를 들어 동영상 URL을 직접 전달하는 대신 HTML 파일에 토큰을 사용했을 것입니다. 예를 들면 다음과 같습니다 $VIDEO_URL$ 토큰 느릅 나무가 될 것

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

진짜 비디오 URL와 런타임시 대체됩니다.

또한 런타임 중에 자산 폴더의 내용을 변경할 수 없으므로 html 파일 내용을 String 변수로로드하고 메서드를 사용하여 토큰을 실제 URL으로 바꾸고 마지막으로 해당 문자열을 다음으로 전달해야합니다. 당신의 웹뷰. 다음과 같은 것이 있습니다 :

//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 자동 재생 : 사실 \t \t \t, \t controlBarAutoHide : 거짓 \t \t \t \t}; 이것은 내 HTML에 내 src 라인입니다. "html = html.replace ("src ", real_video_url); " – sanjay

+0

과 같이 바꿀 수 있습니까?이 경우 제대로 얻었 으면'html = html '을 사용해야합니다. html = html.replace ("src", real_video_url); 대신 "("My Sorce URL ", real_video_url);을 대체하십시오. 'replace' 메쏘드가'real_video_url' 변수의 contnt와 함께 당신의 html 안에 그것들을 모두 repace 할 것이기 때문에, "My Sorce URL"토큰의 다른 시퀀스를 가지지 않는 것을 명심하십시오. – Angelo

+0

Ive는 당신의 길을 시도했다. 그러나 나는 달린다. 검은 스크린 (비디오 플레이 없음)을 보여준다? – sanjay

3

난 내 HTML 동적 뭔가를하고 싶은 경우이 같은 기록 된 동적 부품과 HTML을 것이다 : 내 HTML

<B>%NAME%</B> 

그런 다음 내가로드합니다 : 다음

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

을 그럼 내가 원하는 모든 역학 부품을 교체 할 것입니다 :

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

그러면 웹 뷰로 전달할 것입니다!

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

의 Utils 클래스를 가져올 수 없습니다? 이걸 어디서 얻을 수 있는지 말해 줄래? – Google

1

여기 자산은 무엇을 의미합니까?

문자열 템플릿 = Utils.inputStreamToString (assets.open ("html/template.html"));

+0

context.getAssets() –

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> 

자바 : IOUtils에 관해서는

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

: 나는 다른 방식으로 변수를 전달하기 위해 관리 http://commons.apache.org/proper/commons-io/download_io.cgi

2

.

내 문제는 다른 앱으로 바뀔 때마다 webapp에 올 때 웹보기가 계속 재로드된다는 것입니다. 내 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) 기본 URL로 페이지를로드 할 것입니다을 내가 전화 onPageFinished 변수를 전달하는 새로운 자바 스크립트 메서드. 자바 스크립트에서 는 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 
    } 
} 
관련 문제