我正在尝试将网络视图集成到我的应用中。但我希望网页中的几个部分能够显示在我的网页视图中。为此,我使用Jsoup。我试图解析的网页是circleofcricket.co我已经尝试了许多与Jsoup first,second和其他许多相关的答案。但我不知道为什么从网页中删除页眉和页脚没有任何影响。
我认为这足以回答,但如果需要更多细节可以问。
下面是我的Java类
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
/**
* Created by abhinav on 07-11-2016.
*/
public class ArticlePart2 extends AppCompatActivity{
private WebView webview;
@SuppressLint("JavascriptInterface")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.articlepart2);
webview = (WebView)findViewById(R.id.articleWebView);
/*webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url){
webview.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('navbar navbar-default navbar-static-top')[0].style.display="none"; " +
"})()");
}
});*/
String url="http://circleofcricket.co/2017/Jan/08/Ganguly-lauds-Kohli-for-his-tribute-to-Dhoni/";
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
// hide element by class name
webview.loadUrl("javascript:(function() { " +
"document.getElementsById('myNav')[0].style.display='none';"+" })()");
// hide element by id
/* webview.loadUrl("javascript:(function() { " +
"document.getElementById('your_id')[0].style.display='none';})()");*/
}
});
webview.loadUrl(url);
}
}
答案 0 :(得分:1)
试试这种方式,
得到整个HTML
选择你想要的div
现在在webView中加载这个选定的段,设置mime类型和编码..
Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#yourdiv");
webView.loadData(ele.toString(), "text/html", "utf-8");