2011-11-04 10 views
2

bonjour j'utilise cet exemple http://lexandera.com/2009/01/extracting-html-from-a-webview/ pour obtenir le HTML à partir d'un webview. Mais j'ai besoin de l'utiliser dans ma super classe et je ne sais pas comment le faire. Je peux juste voir le code HTML sur un AlertDialog mais je ne peux pas l'utiliser. Comment puis-je le renvoyer à ma classe principale en tant que String?code html android à partir de webview

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

Répondre

2

effectuer les opérations suivantes:

Mise en place WebView

d'abord ajouter JavaScriptInterface à webView comme suit. Ici, "Android" sera utilisé plus tard pour appeler les fonctions dans JavaScriptInterface plus tard.

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

Classe JavaScriptInterface:

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

fichier 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> 

Appel Java Script Fonction

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

Ajout fonction de rappel dans JavaScriptInterface pour MyAndroid.receiveValueFromJs (val):

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

Ici vous avez retourné HTML dans la variable str. Vous pouvez consulter mon blog: http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/calling-javascript-function-from.html

J'ai trouvé une autre solution: Is it possible to get the HTML code from WebView

+0

c'est la même chose que dans l'exemple, en remplaçant AlertDialog par Toast. Je veux juste avoir accès à la chaîne html après webView.loadUrl dans la classe principale qui n'est pas dans la classe JavaScriptInterface.J'ai essayé dans la classe principale: JavaScriptInterface script = new JavaScriptInterface(); script.loadhtml(); //// class JavaScriptInterface {chaîne publique loadhtml() {return html}} mais ne fonctionne pas –

0

Ne peut pas vous juste faire une variable globale, comme

String globalHtml; 

Et puis attribuez-lui dans la méthode showHTML? Vous pouvez supprimer le code AlertDialog si vous n'en avez pas besoin.

+0

mais j'ai la classe 2 diferent. html est dans la sous-classe. j'ai besoin de le retourner. Merci –

Questions connexes