0

I'm trying to integrate web view in my app. But I want few parts form the web page to be displayed in my web view. For that I'm using Jsoup. The webpage i'm trying to parse is circleofcricket.co I already tried many of the answer related to Jsoup first, second and many others. But I don't know why none of the way has any effect in removing header and footer from the web page.

I think this much is sufficient to answer but if need any more details one can ask.

below is my Java Class

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


}

}

Community
  • 1
  • 1
Abhi Soni
  • 445
  • 6
  • 14
  • What is the problem with the current code? Are there any errors or it doesn't give you the desired results? – Vivek Mishra Jan 09 '17 at 12:21
  • It not having any effect. The header and footer are being displayed as normal web page. But I want to remove them. @Vivek mishra – Abhi Soni Jan 09 '17 at 13:54

1 Answers1

1

Try this way,
get whole html
select the div you want
now load this selected segment in webView, do set mime type and encoding..

Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#yourdiv");
webView.loadData(ele.toString(), "text/html", "utf-8");
  • I tried this it gives an network exception where I define Document object – Abhi Soni Jan 09 '17 at 13:53
  • 1
    thats probably because you cannot execute a network request on main thread... you must execute this in a background thread. You may use AsyncTask for this. – Aman Sharma Jan 10 '17 at 06:17