【问题标题】:Getting Html from webview before rendering在渲染之前从 webview 获取 Html
【发布时间】:2013-02-01 09:38:26
【问题描述】:

我正在使用 android webview 我的目标是在渲染之前解析 html

为此,我在 pagefinish 事件中将以下 javascript 添加到 webview..

public void onPageFinished(WebView view, String url)
{
    view.loadUrl("javascript:varhtmlString=document.getElementsByTagName('html')[0].innerHTML;"
                 +"document.getElementsByTagName('html')[0].innerHTML=window.HTMLOUT.parseHTML(htmlString);");              
}

但问题是在解析 html 之前出现闪回(原始 html)

然后我监控日志,发现 javascript 在 pageFinish 之后执行(异步) 为了使其同步,我使用了等待通知机制并确保 javasript 在页面完成之前运行

但是在解析之前还是会出现原来的html同样的问题

有没有办法在渲染之前更改 html ????

【问题讨论】:

    标签: java android html parsing webview


    【解决方案1】:

    你可以这样做:

    String content = getContentForUrl(url);
    String manipulateHTML  = manipulate(content); // your function to adjust the content.
    
    webView.loadDataWithBaseURL(url, manipulateHTML, "text/html","UTF-8", null);
    
    public String getContentForUrl(String url) {
    
        BufferedReader in = null;
    
        String content = "";
    
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);
    
            in = new BufferedReader(new InputStreamReader(response.getEntity()
                    .getContent()));
    
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
    
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
    
            in.close();
            content = sb.toString();
    
            Log.d("MyApp", "url content for " + url + " " + content);
    
        } catch (Exception e) {
    
            Log.d("MyApp",
                    "url content for " + url + " Exception :" + e.toString());
    
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        return content;
    }
    

    【讨论】:

    • 发帖请求怎么样?
    • 有谁知道 POST、PUT 等是否也可以工作?那么会议等呢?谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2019-04-28
    • 2018-02-19
    • 1970-01-01
    • 2020-03-17
    • 2019-12-31
    • 2020-02-12
    相关资源
    最近更新 更多