//我试图在我的 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/