如何替换HTML文件中的正文

时间:2018-08-10 16:57:38

标签: javascript java android html css

我正在使用android studio

我需要用其他代码替换html文档的正文,这是我尝试遵循的示例

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grafico);

    wv = (WebView) findViewById(R.id.prova);
    wv.setWebViewClient(new MyWebViewClient());

    new BackgroundWorker().execute();

}

// load links in WebView instead of default browser
private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }

    @RequiresApi(21)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return false;
    }
}

private class BackgroundWorker extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        getDarewod();
        return null;
    }

    public void getDarewod() {

        try {
            Document htmlDocument = Jsoup.connect(url).get();
            Element element = htmlDocument.select("#gkHeaderMod > div.grafico-quotazione").first();

            // replace body with selected element


            htmlDocument.body().empty().append(element.toString());
            final String html = htmlDocument.toString();

            uiHandler.post(new Runnable() {
                @Override
                public void run() {
                    wv.loadData(html, "text/html", "UTF-8");
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我想用我的element变量中的代码替换全部内容。但是,此行不起作用。我该怎么办?

htmlDocument.body().empty().append(element.toString());

0 个答案:

没有答案