James Mitchee
James Mitchee

Reputation: 143

webview error on connection and page errors via eclipse

how can i show error message in this when there is no connection at any point during the webview of the website. and also is there any way to hide web page errors like 501, 404 etc. thanks in advance, i am new to the android applications so please be in detail.

package com.website;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
import android.view.Window;
import android.content.Context;
import android.net.NetworkInfo;

public class HelloWebView extends Activity {
    WebView webview;
    /** Called when the activity is first created. */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        WebView.enablePlatformNotifications();
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        webview.loadUrl("http://www.google.com/");
        webview.setWebViewClient(new HelloWebViewClient());

    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true; 
        }
    }



}

Upvotes: 3

Views: 2932

Answers (1)

mudit
mudit

Reputation: 25534

for no connectivity, you can always check for internet connectivity before loading the page. And if internet goes in between or any other error happens then you catch your error in onReceivedError function defined below.

You need to implement one more function in your WebViewClient :

 @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

Using this function, you will get the error code returned by the web page and then you can act accordingly.

Upvotes: 6

Related Questions