I want to extract the html code from WebView and put it in TextView. I search it in internet and I found this code.
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
private TextView textview1;
public MyJavaScriptInterface(TextView textview1)
{
textview1 = textview1;
}
#SuppressWarnings("unused")
public void processContent(String aContent)
{
final String content = aContent;
textview1.post(new Runnable()
{
public void run()
{
textview1.setText(content);
}
});
}
}
webview1.getSettings()
.setJavaScriptEnabled(true);
webview1.addJavascriptInterface
(new MyJavaScriptInterface
(textview1), "INTERFACE");
webview1.loadUrl
("http://blog.depauptits.nl/?m=1");
webview1.setWebViewClient(new
WebViewClient()
{
#Override public boolean ...
shouldOverrideUrlLoading(WebView
view, String url)
{
return true; }
#Override public void onPageStarted
(WebView view, String url, Bitmap favicon)
{ } public void
onPageFinished(WebView view, String url) {
webview1.loadUrl("javascript:window
.LOADHTML.processHTML
('<html>'+document
.getElementsByTagName('html')
[0].innerHTML+'</html>');"); } });
The code is working. The app is running the WebView load its URL, but the TextView doesn't display the html code.
I'm asking your help please correct my code.
you can use jsoup dependency for fetch html from url
this is the implementation of build.gradle :-
implementation 'org.jsoup:jsoup:1.11.3'
String url = "http://www.google.com";
Document document = Jsoup.connect(url).get();
by this way you can get the document object that hold the full html along with ids and tags and all.
for more you can refer :
https://jsoup.org/
This is the tutorial you can refer :
https://www.tutorialspoint.com/jsoup/jsoup_load_url.htm
Related
I have this arraylist:
List<View> mListViews = new ArrayList<View>();
which is populated by
addView(mListViews, "file:///android_asset/a.html");
addView(mListViews, "file:///android_asset/b.html");
addView(mListViews, "file:///android_asset/c.html");
and the addView method:
private void addView(List<View> viewList,String url) {
WebView webView=new WebView(this);
webView.loadUrl(url);
viewList.add(webView);
}
I would like to get the element from the second position in the arraylist as a string. So the expected result is file:///android_asset/b.html. I tried
String test = mListViews.get(1);
But I received error incompatible type, required java.lang.String, found android.view.View.
How to solve this?
If this was my issue, I would do the following:
Create a model class...
public class ListModel {
View view;
String url;
public ListModel (View view, String url) {
this.view = view;
this.url = url;
}
public View getView() {
return view;
}
public void setView(View view) {
this.view = view;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
Make a list that takes in that class...
public class MainActivity extends AppCompatActivity {
List<ListModel> listModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listModel = new ArrayList<>();
listModel.add(new ListModel(view, "url"));
}
}
From there you can create a for loop to get the view and / or the url..
for (int i = 0; i < listModel.size(); i++) {
View getView = listModel.get(i).getView();
String getUrl = listModel.get(i).getUrl();
}
Now, keep in mind, my code will NOT work because I didn't create a VIEW to put into the list. But this is probably the best way to go about it.
Use this:
String test = ( (WebView) mListViews.get(1) ).getUrl();
Be aware that if you do this before a page has finished loading, test will be null.
Another option would be to create a class encapsulating a WebView and the url string, then creating a list of objects of that class.
As you write you have ArrayList of View type.
Result of mListViews.get(1) is type of View. You can't assign View to String variable.
Question is for what you creating ArrayList<View> and populate it with WebView?
If you need the url from the webView I think you just need to do this:
String test = mListViews.get(1).getURL();
I'm trying to create custom error message for my app which is using webview. so far i'm using below code which has a very minimal problem... when i click links on the webview the page is reloading the same page instead of processing the clicked link. my links on the webview are download link so either it should download using the same app or open down-loader options
protected void fetch_web(){
final WebView web;
web = (WebView) findViewById(R.id.voice_mail_view_port);
NoNetwork = (Button) findViewById(R.id.btn_no_internet);
NoNetwork.setVisibility(View.GONE);
String mdb = "http://192.168.23.1/default.php";
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
final Activity MyActivity = this;
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setTitle("Fetching feeds...");
MyActivity.setProgress(progress * 100);
loader();
if(progress == 100) {
MyActivity.setTitle(R.string.app_name);
deLoader();;
}
}
});
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String
failingUrl) {
deLoader();
alert("No Internet Connection","Let's get connected to see feeds");
web.setVisibility(View.GONE);
NoNetwork.setVisibility(View.VISIBLE);
}
});
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl((mdb));
}
protected void loader(){
loading = (ProgressBar) findViewById(R.id.loader);
loading.setVisibility(View.VISIBLE);
}
protected void deLoader(){
loading = (ProgressBar) findViewById(R.id.loader);
loading.setVisibility(View.INVISIBLE);
}
Sample download link http://192.168.23.1/download.php?id=1
Can someone help me out, i suspect my error is coming from here
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String
failingUrl) {
deLoader();
alert("No Internet Connection","Let's get connected to see feeds");
web.setVisibility(View.GONE);
NoNetwork.setVisibility(View.VISIBLE);
}
});
Try forcing the app to open links in other browsers
Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
If it fails let me know to help more.
I try to scroll down the page I have in my WebView. Therefore I found the function .pageDown(true). The problem is that this function does not really work for me. Mostly it does not do anything.
Codesnippet:
wvChat.loadData(chat_, "text/html; charset=utf-8", "UTF-8");
wvChat.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
wvChat.pageDown(true);
}
});
Is there another method or is it wrong to use it in onPageFinished?
get the html content height and use scrollTo(x,y)
wvChat.loadData(chat_, "text/html; charset=utf-8", "UTF-8");
wvChat.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
//use the param "view", and call getContentHeight in scrollTo
view.scrollTo(0, view.getContentHeight());
}
});
I have a solution to scroll down large files (much lines) using the scrollTo function. I set a much higher value than the content height can be.
mWebView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
Handler lHandler = new Handler();
lHandler.postDelayed(new Runnable()
{
#Override
public void run()
{
mWebView.scrollTo(0, 1000000000);
}
}, 200);
}
});
The problem is now, that it does not work for small files (less lines), because the onPageFinished method is called before the WebView has been rendered, so I have to use a delay handle.
wvChat.setWebViewClient(new WebViewClient() {
#Override
public void onPageCommitVisible(WebView view, String url) {
wvChat.pageDown(true);
}
});
this better, without delay...
You can scroll WebView with Javascript:
private fun scrollMove(contentHeight: Float) {
val speed = (contentHeight / 100) * 1000
currentWebView?.evaluateJavascript("\$('html,body').animate({scrollTop:$contentHeight},$speed);", null)
}
I think it's the best solution for scrolling to the bottom
public void scrollToBottom(boolean animate) {
if (animate) {
pageDown(true);
} else {
scrollTo(getScrollX(), computeVerticalScrollRange());
}
}
See https://chromium.googlesource.com/chromium/src.git/+/d0ef9df6be5983f6df7e4e050bbad4eb5030e7a2/android_webview/java/src/org/chromium/android_webview/AwScrollOffsetManager.java#362
As a step in auto-filling some fields in a flow I need to scrape a URL that has its interesting content filled dynamically by JavaScript.
Based on a cars license plates I can look up information on a government website, set an option and a value and submit.
The resulting page will hold the desired content in different tabs so I'll also have to navigate those. (Sorry cant give direct link to this but selecting "Registreringsnummer", using "YN28579" as the value and pressing "Søg" will take you to the page.)
I've done this with a WebViewClient in another Activity so it is possible to navigate the website directly on the android phone/tablet. But in this other Activity I don't want to display the resulting page just scrape some of the data items.
Here is how I've done it with the WebViewClient:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
((Button)findViewById(R.id.btnBack)).setVisibility(View.VISIBLE);
wv = (WebView) findViewById(R.id.wv);
reg = getIntent().getStringExtra("reg");
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
private ProgressDialog pd;
private int count = 0;
#Override
public void onPageFinished(WebView view, String url) {
if (count==1) {
view.loadUrl("javascript:document.getElementById('regnr').checked=true;"
+"document.getElementById('soegeord').value='"+reg+"';"
+"document.getElementById('searchForm').submit();"
+"DMR.WaitForLoad.on();");
} else if (count>=2) {
view.loadUrl("javascript:document.body.innerHTML " +
"= '<div class=\"tabNav\">'+document.getElementsByClassName('tabNav')[0].innerHTML+'</div>';" +
"document.getElementsByClassName('h-tab-content')[0].style.width='320px';" +
"document.getElementsByClassName('h-tab-btns')[0].style.width='320px';" +
"document.getElementsByClassName('h-tab-btns')[0].style.height='45px';" +
"document.getElementsByTagName('ul')[0].style.display='inline';" +
"document.head.appendChild='<meta name=\"viewport\" content=\"width=device-width\">';" +
"document.body.style.minWidth ='300px';");
if (pd!=null) {
pd.dismiss();
}
view.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (pd==null || !pd.isShowing()) {
pd = ProgressDialog.show(SkatActivity.this, "cars.dk", "Vent venligst...", true, false);
}
count++;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("https://motorregister.skat.dk/dmr-front/appmanager/skat/dmr?_nfpb=true&_nfpb=true&_pageLabel=vis_koeretoej_side&_nfls=false");
}
I can't seem to figure out how to incorporate this in
Do you set the WebView as invisible? Found this question that seems to be working along those same lines, but I can't figure out how to get the working WebViewClient hidden but usable inside this other activity?
This is a part of my android app, i have created a webview...but when i click on any link inside the webview...nothing happens....here is my code...i want to launch the link either in any browser or any installed download manager...i m a newbie..please help me out with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
setupActionBar();
// no need to use title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set webview as main content only
mWeb = new WebView(this);
setContentView(mWeb);
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// set url for webview to load
mWeb.loadUrl("http://vesit-d7b.host22.com/Assignments/ECCF.html");
}
Please use this code and check that your url is being caught by the webview or not.
myweb.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
Toast.makeText(this,url,Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
see what url is coming when you click the link.....
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v("here","the link is ::" + url);
view.loadUrl(url);
return true;
}
and then copy it on browser to see whether it is a valid link or not..
#Ritesh remove the shouldOverrideUrlLoading method it will work