在Android中使用Jsoup隐藏HTML标记

时间:2018-02-14 22:46:34

标签: android parsing webview jsoup

我正在使用Android应用程序。我希望只能从网站加载项目并隐藏一些标签(页眉,页脚,搜索面板)。我使用了Webview和Jsoup。一切正常。但是当我点击某个项目时,在新页面中将再次打开页眉/页脚。如何使用此代码隐藏页眉和页脚?

public class MainActivity extends AppCompatActivity {

WebView webview;
String url="http://hut.az/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview= (WebView) findViewById(R.id.webview);



    new MyAsynTask().execute();

}

private class MyAsynTask extends AsyncTask<Void, Void, Document> {
    @Override
    protected Document doInBackground(Void... voids) {

        Document document = null;

        try {
            document= Jsoup.connect(url).get();
            document.getElementsByClass("header1").remove();
            document.getElementsByClass("topbar clearfix").remove();
            document.getElementsByClass("mapandslider").remove();
            document.getElementsByClass("footer1").remove();
            document.getElementsByClass("copyright").remove();


        } catch (IOException e) {
            e.printStackTrace();
        }
   return document ;
    }

    @Override
    protected void onPostExecute(Document document) {
        super.onPostExecute(document);

        webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
        webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
        webview.setWebViewClient(new WebViewClient(){
            private WebView view;
            private WebResourceRequest request;

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                this.view = view;
                this.request = request;
                view.loadUrl(url);

                return super.shouldOverrideUrlLoading(view, request);
            }



        });

    }
}
}

打开应用时:

When opens app

当我点击任何项目时,页眉和页脚会再次显示 When I click any item, header and footer shows again

1 个答案:

答案 0 :(得分:0)

这是因为您在/channel中打开了一个新页面,然后您没有删除该新网页中的标记,您需要做的就是将代码更改为

Webview