I am able to load a html page in a webview successfully. I can also get the url which is clicked on webview by setting the webviewclient. But what i want is something diffrent, i have a webview which loads a web page and also a button on click of which i want all the urls present in that web page. How do i do this?
So far the code i have tried is
dialog = ProgressDialog.show(Activity.this, null, null);
dialog.setContentView(R.layout.loader);
w.setWebViewClient(new MyWebViewClient());
w.loadDataWithBaseURL("same://ur/l/tat/does/not/work", string_html,
"text/html", "utf-8", null);
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (url.startsWith("http://")) {
System.out.println("INSIDE IF"); // NON-NLS
view.stopLoading();
urlToSend = url;
System.out.println("in " + urlToSend);
Intent intent = new Intent(DetailNews.this, Web.class);
intent.putExtra("Web", url);
startActivity(intent);
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dialog.dismiss();
}
}
If you want to get all urls on page you need html parser. Try jsoup
If you don’t want to use external libs on your project you can create own parser.
1) Load source page via HttpUrlConnection or HttpClient
2) Use some reader to iterate page content by line
3) Create regexp expression to extract links.
Similar post