【问题标题】:How can I remove paragraph tag from Url loaded from Webview in Android Studio?如何从 Android Studio 中的 Webview 加载的 Url 中删除段落标签?
【发布时间】:2020-03-17 18:28:33
【问题描述】:

// 我试图从这个http://www.mcxlivedata.in/ 这个url 在我的webview 中只显示mcx 实时数据,但现在我很困惑如何删除不需要的段落,或者我可以使用getElementTag 在我的webview 中只显示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);


                }
            });

        }
    }
}

【问题讨论】:

    标签: java android android-studio webview jsoup


    【解决方案1】:

    这应该是正确的做法

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

    【讨论】:

    • 它不工作,它可以删除我必须显示的所有内容,只显示 mcx 率可以请帮助我如何解决这个问题
    猜你喜欢
    • 2014-02-26
    • 2014-01-07
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多