how to get html content from a webview?

后端 未结 12 1540
鱼传尺愫
鱼传尺愫 2020-11-22 04:29

Which is the simplest method to get html code from a webview? I have tried several methods from stackoverflow and google, but can\'t find an exact method. Please mention an

相关标签:
12条回答
  • 2020-11-22 04:55

    One touch point I found that needs to be put in place is "hidden" away in the Proguard configuration. While the HTML reader invokes through the javascript interface just fine when debugging the app, this works no longer as soon as the app was run through Proguard, unless the HTML reader function is declared in the Proguard config file, like so:

    -keepclassmembers class <your.fully.qualified.HTML.reader.classname.here> {
        public *; 
    }
    

    Tested and confirmed on Android 2.3.6, 4.1.1 and 4.2.1.

    0 讨论(0)
  • 2020-11-22 04:57

    Why not get the html first then pass it to the web view?

    private String getHtml(String url){
        HttpGet pageGet = new HttpGet(url);
    
        ResponseHandler<String> handler = new ResponseHandler<String>() {
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                String html; 
    
                if (entity != null) {
                    html = EntityUtils.toString(entity);
                    return html;
                } else {
                    return null;
                }
            }
        };
    
        pageHTML = null;
        try {
            while (pageHTML==null){
                pageHTML = client.execute(pageGet, handler);
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return pageHTML;
    }
    
    @Override
    public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
        mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
        webview.getSettings().setJavaScriptEnabled(true);
        WebViewClient anchorWebViewClient = new WebViewClient()
        {
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
    
                //Do what you want to with the html
                String html = getHTML(url);
    
                if( html!=null && !url.equals(lastLoadedURL)){
                    lastLoadedURL = url;
                    webview.loadDataWithBaseURL(url, html, null, "utf-8", url);
                }
    }
    

    This should roughly do what you want to do. It is adapted from Is it possible to get the HTML code from WebView and shout out to https://stackoverflow.com/users/325081/aymon-fournier for his answer.

    0 讨论(0)
  • 2020-11-22 04:58

    try using HttpClient as Sephy said:

    public String getHtml(String url) {
        HttpClient vClient = new DefaultHttpClient();
        HttpGet vGet = new HttpGet(url);
        String response = "";    
    
        try {
            ResponseHandler<String> vHandler = new BasicResponseHandler();
            response = vClient.execute(vGet, vHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    
    0 讨论(0)
  • 2020-11-22 04:59

    Actually this question has many answers. Here are 2 of them :

    • This first is almost the same as yours, I guess we got it from the same tutorial.

    public class TestActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);
            final WebView webview = (WebView) findViewById(R.id.browser);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
    
            webview.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
                            "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
                }
            });
    
            webview.loadUrl("http://android-in-action.com/index.php?post/" +
                    "Common-errors-and-bugs-and-how-to-solve-avoid-them");
        }
    
        class MyJavaScriptInterface {
    
            private Context ctx;
    
            MyJavaScriptInterface(Context ctx) {
                this.ctx = ctx;
            }
    
            public void showHTML(String html) {
                new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                        .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
            }
    
        }
    }
    

    This way your grab the html through javascript. Not the prettiest way but when you have your javascript interface, you can add other methods to tinker it.


    • An other way is using an HttpClient like there.

    The option you choose also depends, I think, on what you intend to do with the retrieved html...

    0 讨论(0)
  • 2020-11-22 05:01

    Android WebView is just another render engine that render HTML contents downloaded from a HTTP server, much like Chrome or FireFox. I don't know the reason why you need get the rendered page (or screenshot) from WebView. For most of situation, this is not necessary. You can always get the raw HTML content from HTTP server directly.

    There are already answers posted talking about getting the raw stream using HttpUrlConnection or HttpClient. Alternatively, there is a very handy library when dealing with HTML content parse/process on Android: JSoup, it provide very simple API to get HTML contents form HTTP server, and provide an abstract representation of HTML document to help us manage HTML parsing not only in a more OO style but also much easily:

    // Single line of statement to get HTML document from HTTP server.
    Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
    

    It is handy when, for example, you want to download HTML document first then add some custom css or javascript to it before passing it to WebView for rendering. Much more on their official web site, worth to check it out.

    0 讨论(0)
  • 2020-11-22 05:03

    In KitKat and above, you could use evaluateJavascript method on webview

    wvbrowser.evaluateJavascript(
            "(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
             new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String html) {
                    Log.d("HTML", html); 
                    // code here
                }
        });
    

    See this answer for more examples

    0 讨论(0)
提交回复
热议问题