How to retrieve HTML content from WebView (as a string)

前端 未结 6 2138
旧巷少年郎
旧巷少年郎 2020-11-30 00:55

How do I retrieve all HTML content currently displayed in a WebView?

I found WebView.loadData() but I couldn\'t find the opposite equivalent (e.g. WebVi

6条回答
  •  情深已故
    2020-11-30 01:08

    Add this to your code:

    private String getUrlSource(String site) throws IOException {
        //GNU Public, from ZunoZap Web Browser
        URL url = new URL(site);
        URLConnection urlc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
        urlc.getInputStream(), "UTF-8"));
        String inputLine;
        StringBuilder a = new StringBuilder();
        while ((inputLine = in.readLine()) != null)
        a.append(inputLine);
        in.close();
    
        return a.toString();
    }
    

    then lets say you what to get Google's source you would do:

    getURLSource("http://google.com");
    

提交回复
热议问题