How can I remove paragraph tag from Url loaded from Webview in Android Studio?

别来无恙 提交于 2021-01-29 12:37:38

问题


// I am trying to display only mcx live data in my webview from this http://www.mcxlivedata.in/ this url but now I am very confuse how can I remove unwanted paragraph or can I show only mcx table in my webview using getElementTag please help me to fix that problem thanks for advance..

package com.tech.jkjewellers;


import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; 
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;    
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;    
import java.io.IOException;


public class McxActivity extends AppCompatActivity {
    String url = "http://www.mcxlivedata.in/";
    WebView webView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mcx);

        webView = findViewById(R.id.web_view);
        new MyAsynTask().execute();    
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl(url);

    }

//using jsoup for removing tags

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

            Document document = null;
            try {
                document = Jsoup.connect(url).get();
                document.getElementById("masthead").remove();
                document.getElementsByClass("site-footer clearfix").remove();
                document.getElementsByTag("<p>").remove();

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

        @Override
        protected void onPostExecute(final 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() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    return true;
                }

                @Override
                public void onPageFinished(WebView view, String url) {

                    super.onPageFinished(view, url);


                }
            });

        }
    }
}

回答1:


This should be correct approach

document = Jsoup.connect(url).get();
            document.getElementById("masthead").remove();
            document.getElementsByClass("site-footer").remove(); 
            document.getElementsByTag("p").remove();


来源:https://stackoverflow.com/questions/60728244/how-can-i-remove-paragraph-tag-from-url-loaded-from-webview-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!