问题
// 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