I'm trying to load a URL to a webView, but Some time I'm Receiving net::ERR_CONTENT_LENGTH_MISMATCH error in onReceivedError method and the page stopped loading.
The URL is one time generated by the backend and has a lot of redirection.
my webViewClinet :
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
ToastUtil.showToast(error, getContext());
}
Can anyone help me?
Related
I'm getting the URL and finishing the activity when the URL is stackoverflow.com. How do I pass this URL to Parent Activity?
wvPayment.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "shouldOverrideUrlLoading: "+String.valueOf(request.getUrl()));
if(String.valueOf(request.getUrl()).equals("http://stackoverflow.com/")){
finish();
Toast.makeText(PaymentActivity.this ,"Successful",Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
wvPayment.setVisibility(View.GONE);
ProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wvPayment.setVisibility(View.VISIBLE);
ProgressBar.setVisibility(View.GONE);
}
});
In iPhone we have functions like webViewDidStartLoad and webViewDidFinishLoad to check the Start of loading and finish of particular url.
Do we have anythng like this on Android ?
You could try something like this :
webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Do something here
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Do something here
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// Do something here
}
});
Yes you can use WebViewClient :
private class CustomWebClient extends WebViewClient{
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
}
public void onPageFinished(WebView view, String url)
{
}
}
Usage:
webView.setWebViewClient(new CustomWebClient());
Every method name is self explanatory and you can also check this : http://developer.android.com/reference/android/webkit/WebViewClient.html
I want to trigger a function after webview's finished loading a webpage and received no error. According to my experiment, the onPageFinished() will be triggered several times and in most of the cases, it will be executed before onReceivedError().
So how can I know that the webpage is loaded and no error is received?
You can set the WebViewClient in the WebView and Override its Callback methods which will let you know when the webpage has finished loading the page. For example
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
if(progess!=null){
progess.dismiss();
}
progess = ProgressDialog.show(LoadUrl.this, "", "Loading please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (Uri.parse(url).getHost().equals("callback")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);
// Add YOUR CODE HERE
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(LoadUrl.this, "Sorry! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
}
Here onPageFinished(WebView view, String url) will be called when webView has finished loading the current Page.
Hope it helps :)
I have a webview in my app, however sometimes due to connectivity the webview fails to load and I get the default webpage unavailable page. I want to show an alertdialog if the webview failed to load. Is there anyway I can check (maybe in the shouldOverridePageLoad function) that a webview loaded successfully? Thanks again
Use a WebClient on your web view as follow :
webView.setWebViewClient(new WebViewClient(){
#Override public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
// Do something
}
});
Extending on Damien's answer on using WebViewClient, there are four listeners available on WebViewClient to check for the success and failure of loading web pages.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
}
#Override
public void onReceivedHttpError(
WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
}
});
There is also:
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
which is deprecated in favor of its overload mentioned in the above code.
I'm looking to develop an application to track how long a site takes to load on the default android browser. Does the browser have any method of tracking when the site is fully loaded?
on page started and on page finished are the methods to
get the the start time and end time of loading of the webview
WebView search_webview;
search_webview = (WebView) findViewById(R.id.show_webview);
search_webview.setWebViewClient(new 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);
}
#Override
public void onPageFinished(WebView view, String url) {
_dialog.dismiss();
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try{
_dialog.dismiss();
Toast.makeText(ShowWebView.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
}catch (Exception e) {
// TODO: handle exception
}
}
});