how to get html content from a webview?

后端 未结 12 1828
鱼传尺愫
鱼传尺愫 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 05:16

    I would suggest instead of trying to extract the HTML from the WebView, you extract the HTML from the URL. By this, I mean using a third party library such as JSoup to traverse the HTML for you. The following code will get the HTML from a specific URL for you

    public static String getHtml(String url) throws ClientProtocolException, IOException {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet, localContext);
            String result = "";
    
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                    response.getEntity().getContent()
                )
            );
    
            String line = null;
            while ((line = reader.readLine()) != null){
                result += line + "\n";
            }
            return result;
        }
    

提交回复
热议问题