2011-11-04 63 views
2

你好我正在使用这个例子http://lexandera.com/2009/01/extracting-html-from-a-webview/从webview中获取HTML。但我需要在我的超类中使用它,我不知道如何做到这一点。我只能看到AlertDialog上的html,但我不能使用它。我怎样才能返回到我的主类作为字符串?来自webview的android html代码

final Context myApp = this; 

/* An instance of this class will be registered as a JavaScript interface */ 
class MyJavaScriptInterface 
{ 
    @SuppressWarnings("unused") 
    public void showHTML(String html) 
    { 
     new AlertDialog.Builder(myApp) 
      .setTitle("HTML") 
      .setMessage(html) 
      .setPositiveButton(android.R.string.ok, null) 
     .setCancelable(false) 
     .create() 
     .show(); 
    } 
} 

final WebView browser = (WebView)findViewById(R.id.browser); 
/* JavaScript must be enabled if you want it to work, obviously */ 
browser.getSettings().setJavaScriptEnabled(true); 

/* Register a new JavaScript interface called HTMLOUT */ 
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT"); 

/* WebViewClient must be set BEFORE calling loadUrl! */ 
browser.setWebViewClient(new WebViewClient() { 
    @Override 
    public void onPageFinished(WebView view, String url) 
    { 
     /* This call inject JavaScript into the page which just finished loading. */ 
     browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');"); 
    } 
}); 

/* load a web page */ 
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html"); 

回答

2

方法如下:

设置的WebView

首先添加JavaScriptInterface到web视图如下。这里稍后会使用“Android”在稍后的JavaScriptInterface中调用函数。

webView = (WebView) findViewById(R.id.webView); 
    WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webView.addJavascriptInterface(new JavaScriptInterface(this), "MyAndroid"); 

的JavaScriptInterface类:

public class JavaScriptInterface { 
    Context mContext; 
    JavaScriptInterface(Context c) { 
     mContext = c; 
    } 
} 

HTML文件:

<html> 
    <head> 
    <script type="text/javascript"> 
    function getValue() 
      { 
     var x=document.getElementById("content").innerHTML; 
     //  alert(x); 
     MyAndroid.receiveValueFromJs(x); 
      } 
    </script> 
    </head> 
    <body> 
    <div id="content"> 
    This is html content <hr/> 
    Other contents; 
    <input type="button" onclick="getValue()" value="Get Content" /> 
    </div> 
    </body> 
    </html> 

中调用Java脚本函数

//this calls javascript function 
    webView.loadUrl("javascript:getValue()"); 
在JavaScriptInterface

添加回调函数MyAndroid.receiveValueFromJs(VAL):

public void receiveValueFromJs(String str) { 
    Toast.makeText(mContext, "Received Value from JS: " + str,Toast.LENGTH_SHORT).show(); 
} 

在这里,你已经在str变量返回的HTML。

有关详细信息,请参阅我的博客:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/calling-javascript-function-from.html

我发现了另一种解决方案:Is it possible to get the HTML code from WebView

+0

这与在示例中用Toast替换AlertDialog是一样的。我只想在主类中的webView.loadUrl之后访问html字符串,而不是在类JavaScriptInterface中。 script.loadhtml(); //// class JavaScriptInterface {public string loadhtml(){return html}}但不起作用 –

0

你不能只是做一个全局变量,像

String globalHtml; 

然后在showHTML方法分配呢?

globalHtml = html; 

如果您不需要它,您可以删除AlertDialog代码。

+0

但我有2个不同的类。 html在子类中。我需要回报它。谢谢 –