2

I am trying to extract small portion from webpage and load into webview I have tried following solution given in the link,But it did not work

Display a part of the webpage on the webview android

Extracting data using getElementsByClass("darewod")

htmlDocument = Jsoup.connect(htmlPageUrl).get(); element = htmlDocument.getElementsByClass("darewod"); String html = element.toString(); String mime = "text/html"; String encoding = "utf-8"; 

I have tried the following two methods to load to webview but it seems not working,Its just printing HTML on UI

 wv1.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); wv1.loadData(html, "text/html", null); 

Can you please tell me if i am missing anything here?

2
  • Can you either share the url or the content of String html? Problem might be: html isn't a valid html document anymore and is missing all head definitions (CSS, etc.). Commented Nov 8, 2016 at 8:18
  • Html has this value <div class="darewod"> <a title="Workout of the Day" href="/workouts/lower-abs-workout.html" rel="alternate"><img src="/images/grid/wod/2016/wod_nov8.jpg" alt="Workout of the Day"></a> </div> Is this valid html? Commented Nov 8, 2016 at 20:21

1 Answer 1

2

Your are loading your html code without the proper structure (so all definitions in head are lost, like CSS references) and without the initial document (or loading with base url) all relative paths are broken.

<div class="darewod"> <a title="Workout of the Day" href="/workouts/lower-abs-workout.html" rel="alternate"><img src="/images/grid/wod/2016/wod_nov8.jpg" alt="Workout of the Day"></a> </div> 

What you could do: replace the body of your document with your selected element, therby preserving the structure and information regarding base:

Example Code

WebView wv; Handler uiHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv = (WebView)findViewById(R.id.webView); wv.setWebViewClient(new MyWebViewClient()); new BackgroundWorker().execute(); } // load links in WebView instead of default browser private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return false; } @RequiresApi(21) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { view.loadUrl(request.getUrl().toString()); return false; } } private class BackgroundWorker extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { getDarewod(); return null; } public void getDarewod(){ try { Document htmlDocument = Jsoup.connect("http://darebee.com/").get(); Element element = htmlDocument.select("#gkHeaderMod > div.darewod").first(); // replace body with selected element htmlDocument.body().empty().append(element.toString()); final String html = htmlDocument.toString(); uiHandler.post(new Runnable() { @Override public void run() { wv.loadData(html, "text/html", "UTF-8"); } }); } catch (IOException e) { e.printStackTrace(); } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

@suv It would probably be better to open a new question.
Well I have already solved it and in my case there is no need of this structure. What i was missing is setting javascript enabled to true.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.