java - 如何从 Android Studio 中的 Webview 加载的 URL 中删除段落标签?

标签 java android android-studio webview jsoup

//我试图在我的 web View 中仅显示 mcx 实时数据 http://www.mcxlivedata.in/这个网址,但现在我很困惑如何删除不需要的段落,或者我可以使用 getElementTag 在我的 web View 中仅显示 mcx 表,请帮助我解决该问题,谢谢您的帮助..

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);

    }

//使用jsoup删除标签

    @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);


                }
            });

        }
    }
}

最佳答案

这应该是正确的方法

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

关于java - 如何从 Android Studio 中的 Webview 加载的 URL 中删除段落标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60728244/

相关文章:

android - 使用Robotiumin如何检查或制作测试用例以检查android中的网络连接可用性?

android-studio - 适用于Android的 Telegram ,令人困惑的NDK错误

android - 如何添加带有 fragment 的新选项卡

javascript - 将 JavaScript 值从 Webview 传递到 Activity?Android

安卓工作室 3.1 : Erroneous unresolved references in editor

android studio 找不到指定的文件

java - 无需 IDE 的 ucanaccess jdbc 连接

java - 在项目中用java显示图像

java - 文件名中指定的逗号(,)导致curl处理器无法发送请求

Java Main 方法,良好的编码风格