Im trying to load a URL using WebView, but the web view does not seem stable - sometimes the page loads 100%, sometimes it stops loading on 30%/50%/75% (random).
I have the WebView being created inside an Android Bolts task, and the WebView is not being showed for the user, it only loads some javascript for future requests.
Code:
private Task<Boolean> task_SetupJavascript(){
final Task<Boolean>.TaskCompletionSource myTask = Task.create();
WebView webview = new WebView(getActivity());
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: LocalStorage.set('valueA', 'valueB', 'valueC');");
myTask.setResult(Boolean.TRUE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// never fired during tests
Log.e(TAG,"onReceivedError ---> Something went wrong... ");
myTask.setCancelled();
}
});
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.e(TAG, ""+newProgress);
}
});
webview.loadUrl(URL);
webview.onResume();
return myTask.getTask();
}
I can't figure out what can be causing the WebView to stop loading randomly in the middle of the progress.
Sometimes it fails the first time it runs this task, sometimes it fails after the 3/4/5 time.
Related
I am using webview for showing Flipkart website in my application.
Everything is working fine as needed except one thing:
when I click on BuyNow button, select variant view gets visible.
This view comes for 1 sec and then show the white blank page.
Is there any setting i need to enable in webview to show this types of view?
I had already gone through many questions on Stackoverflow but nothing helped me.
I tried setting webchromeclient and below methods on webview with no success.
shopping_webview.getSettings().setAllowFileAccess(true);
shopping_webview.getSettings().setAllowContentAccess(true);
shopping_webview.getSettings().setAllowFileAccessFromFileURLs(true);
shopping_webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
shopping_webview.getSettings().setDomStorageEnabled(true);
shopping_webview.getSettings().setUseWideViewPort(true);
shopping_webview.getSettings().setLoadWithOverviewMode(true);
shopping_webview.getSettings().setLoadsImagesAutomatically(true);
shopping_webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
shopping_webview.getSettings().setSupportMultipleWindows(true);
shopping_webview.getSettings().setBuiltInZoomControls(true);
shopping_webview.getSettings().setDisplayZoomControls(false);
shopping_webview.getSettings().setJavaScriptEnabled(true);
webviewLink.getSettings().setLoadsImagesAutomatically(true);
webviewLink.getSettings().setJavaScriptEnabled(true);
webviewLink.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webviewLink.loadUrl(postlink);
webviewLink.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO show you progress image
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO hide your progress image
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
load the Url with this way it's work for me in the same app..
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(_URL);
}
private class MyWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
}
I know how to get notified when it finishes loading a web page, but is there any way to know when it starts loading a new one that is initiated from a link from the original web page?
The reason that i want it is to make a ProgressBar visible whenever a page starts loading, and make it invisible whenever it finishes.
UPDATE: What I'm asking is if it's possible to know when a new page starts loading a page. Although from the link I found onPageStarted, and it works.
Check with the following
webView.setWebViewClient(new WebViewClient() {
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
//Show loader on url load
}
public void onPageFinished(WebView view, String url) {
//cancel loader complted
}
});
// Set Javascript enabled on webview
webView.getSettings().setJavaScriptEnabled(true);
To listen for a web page starting loading you should override onPageStarted, to listen for finishing you should override onPageFinished
and just to make it more complete, listen for error with onReceivedError
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
// runs when a page starts loading
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// page finishes loading
progressBar.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
// runs when there's a failure in loading page
progressBar.setVisibility(View.GONE);
Toast.makeText(context, "Failure on loading web page", Toast.LENGTH_SHORT).show();
}
});
I have gone through every related post I found here and still can't get it to work.
I'm opening my WebView with a URL that has redirects (sometimes many redirects) and I want to know when the loading is really finished (URL finished loading with no more redirects).
So any suggestions?
Every post I read said to override shouldOverrideUrlLoading but no matter what I do shouldOverrideUrlLoading does not get called.
My code:
mWebView = (WebView) findViewById(R.id.webview);
WebSettings settings = mWebView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new MyWebViewClient());
mUrl = "http://cdn.something.com/something.html";
// mUrl = "https://www.google.com";
mWebView.loadUrl(mUrl);
public class MyWebViewClient extends WebViewClient {
public MyWebViewClient() {}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "WebViewClient: shouldOverrideUrlLoading");
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d(TAG, "WebViewClient: onPageStarted: url: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "WebViewClient: onPageFinished: url: " + url);
super.onPageFinished(view, url);
}
}
Thank you
you are overriding the new shouldOverrideUrlLoading method which was added in android N,
you have to override both methods to make it work (old one which is deprecated and new one)
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(uri);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(uri);
}
refer this
,and onPageFinished will be called when page has finished loading
In my case WebViewClient wasn't showing if there was changes on the webview, I supposed that is something about the web that is been running.
I could get that information from the WebChromeClient with OnProgressChanged, I don't know if this would help people anyway, but here is the code:
webview.webChromeClient = object : WebChromeClient(){
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
if (newProgress == 100) {
Log.d("testing", webview.getOriginalUrl())
Log.d("testing", webview.url)
}
}
}
This way I could know what is been loaded and when is finished.
I want to be able to recognize when an error occurs and tell show a toast message with the error but I can never get them to call.
I also want to be able to tell when the webview starts navigating to a url and detect which url it's going to.
Unfortunately it just seems to ignore everything and I have no idea how to start a function or method or anything once this occurs.I don't really care how I go about determining when a page starts loading or an error occurs I just need a way that works.
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient()
{
private boolean error;
public void onPageStarted(WebView view, String url, Bitmap favicon) {
onPageStarted(view, url, favicon);
Toast.makeText(getApplicationContext(), "page started:" + url, Toast.LENGTH_SHORT).show();
error = false;
}
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) {
error = true;
System.out.println("description error" + description);
view.setVisibility( View.GONE );
Toast.makeText(getApplicationContext(), "error:" + description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView view, String url) {
if (!error) {
view.setVisibility( View.VISIBLE );
}
error = false;
Toast.makeText(getApplicationContext(), "page finished" + url, Toast.LENGTH_SHORT).show();
}
}
);
myWebView.setBackgroundColor(0);
WebSettings webSettings = myWebView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowFileAccess(true);
// Other webview settings
myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
myWebView.loadUrl("https://example.com/");
}
}
The onPageStarted(), onPageFinished(), and onReceivedError() methods are not defined in the WebChromeClient class. They're members of WebViewClient. You subclassed the wrong one.
If you add the #Override annotation above each method, your IDE should yell at you that those methods don't override anything in the super class, which is why that annotation is useful.
Simply move those overrides to the WebViewClient instance.
myWebView.setWebViewClient(new WebViewClient() {
private boolean error;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
...
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
...
}
#Override
public void onPageFinished(WebView view, String url) {
...
}
}
);
myWebView.setWebChromeClient(new WebChromeClient());
I use WebView to load urls and also send and receive Java script calls.
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
showContentOrLoadingIndicator(true);
}
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
The code works fine on android < 5 but on lollipop versions sometimes the app simply get totally stuck and sometimes crash.
Any idea why?