WebView Page Not Found Error Trapping

by Craigbtx » Tue, 15 Feb 2011 02:21:21 GMT


Sponsored Links
 I am using webview. If the page is not correct or the website is down,
how can I error trap a page not found and show the users an error page
or some

othe page?

I am very much a beginner so I need examples of code.


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView webview = new WebView(this);
        setContentView(webview);

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);

        // No error trapping
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl(" http://www.MyWebsite.com ");

-- 



Re: WebView Page Not Found Error Trapping

by Mark Murphy » Tue, 15 Feb 2011 02:25:17 GMT


 Implement onReceivedError() in your WebViewClient.






-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

Warescription: Three Android Books, Plus Updates, One Low Price!

-- 


Sponsored Links


Re: WebView Page Not Found Error Trapping

by Craigbtx » Tue, 15 Feb 2011 04:23:15 GMT


 I have the OnReceivedError event
I get the dialog Internet Down message but the page not found still
shows in hte background.
Is there some way around this? Like show a blank screen saying "error:
Site down. Try later" or something?

Thanks


@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Needed for progress bar
        // Let's display the progress in the activity titlebar, like
the browser app does
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.main);

        // WORKS WITH PROGRESS BAR
        WebView webview = new WebView(this);
        setContentView(webview);
        WebSettings webSettings = webview.getSettings();

        setProgressBarVisibility(true);

        webview.getSettings().setJavaScriptEnabled(true);


        // Make the zoom controls visible
        webSettings.setBuiltInZoomControls(true);

            final Activity activity = this;
            webview.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                        activity.setProgress(progress * 100 );

                }
            });


            webview.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String
description, String failingUrl) {
                  Toast.makeText(activity, "Internet connection down? " +
description, Toast.LENGTH_SHORT).show();
                }
            });


            // Go to a web page
            webview.loadUrl(" http://www.MySite.com/Default.aspx" ;);

    }









-- 



Re: Re: WebView Page Not Found Error Trapping

by Mark Murphy » Tue, 15 Feb 2011 04:26:24 GMT


 


Call loadUrl() or loadData() or loadDataWithBaseURL() with whatever you want.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

Android Training...At Your Office:  http://commonsware.com/training 

-- 



Re: WebView Page Not Found Error Trapping

by Craigbtx » Tue, 15 Feb 2011 04:51:23 GMT


 Can you give me some sample code? I am learning and only done android
programming for 2-3 weeks.







-- 



Re: WebView Page Not Found Error Trapping

by Craigbtx » Tue, 15 Feb 2011 20:52:03 GMT


 I got it to work

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Let's display the progress in the activity titlebar, like
the browser app does
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.main);


        WebView webview = new WebView(this);
        setContentView(webview);
        WebSettings webSettings = webview.getSettings();

        setProgressBarVisibility(true);

        webview.getSettings().setJavaScriptEnabled(true);


        // Make the zoom controls visible
        webSettings.setBuiltInZoomControls(true);

            final Activity activity = this;
            webview.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                        activity.setProgress(progress * 100 );

                }
            });


        // Creating and setting a WebViewClient subclass.
        // It will be called when things happen that impact the
rendering of the content,
        // eg, errors or form submissions. You can also intercept URL
loading here (via shouldOverrideUrlLoading()).

            webview.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String
description, String failingUrl) {
                  Toast.makeText(activity, "Internet connection down? " +
description, Toast.LENGTH_SHORT).show();


                        view.loadData("Your internet connection may be
down? Please restart your app.", "text/html", "UTF-8");


                }
            });



            // Go to a web page
            webview.loadUrl(" http://www.MyWebsite.com ");


    }
}






-- 



Re: WebView Page Not Found Error Trapping

by Jonathan Pettersson » Wed, 16 Feb 2011 04:46:29 GMT


 Like Mark said:

mWebView.setWebViewClient(new WebViewClient() {
         public void onReceivedError(WebView view, int errorCode, String
description, String failingUrl) {
         //do something on error
         }
});

The WebView will show a default error message and due to a bug in Android,
this message is tricky to hide. I solved it by setting the parent view
visibility to GONE and calling mWebView.setData with an empty string.

Google code page for the android issue:
 http://code.google.com/p/android/issues/detail?id=2340& ;q=webview%20error&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

Best,
J





-- 



Re: WebView Page Not Found Error Trapping

by whatever » Fri, 25 Feb 2011 12:57:16 GMT


 SOLVED

Ok i got it to work first go..took me longer to write this post, lucky
guess!
Don't know how i did it- i'm an old procedural programmer...don't know
java or OO
you guys don't know how easy programming used to be..!

i digress, ok you replace the WebViewClient with you own
class, MyWebViewClient intercept the error before it happens..

hope it helps...
i tested this on android 1.5!

        private class MyWebViewClient extends WebViewClient {
                    @Override
            public void onReceivedError(WebView view, int errorCode, String
                                description, String failingUrl) {
                                         //do something on error
                                                 
//view.loadUrl("file:///android_asset/
index.htm");
                                                 view.loadData("Your internet 
connection may be
down? Please restart your app.", "text/html", "UTF-8");


                                         }





        }


// normal OO malarky

                   myWebView = (WebView) findViewById(R.id.webview);

           myWebView.setWebViewClient(new MyWebViewClient());// magic
here



           WebSettings webSettings = myWebView.getSettings();

           webSettings.setJavaScriptEnabled(true);//enable java script
           myWebView.loadUrl("file:///android_asset/
page_dosent_exist.htm");// url non sense

-- 



Re: WebView Page Not Found Error Trapping

by whatever » Fri, 25 Feb 2011 12:57:35 GMT


 test

-- 



Other Threads

1. Service: both onStart() and onStartCommand() called

Hello (World).

I'm trying to create a Service which is backwards compatible, working
on both pre-2.0 and post-2.0 phones.
Therefore I implement both onStart() and onStartCommand() according
to: 
http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent,
int, int).
Where the comments on onStart() say:
// This is the old onStart method that will be called on the pre-2.0
// platform.  On 2.0 or later we override onStartCommand() so this
// method will not be called.

However, running 2.1 in the Emulator, both methods are called when I
start my Service. Is this a bug? Did anyone else have this problem?

-- 

2. RIL issue: is libreference-ril.so reliable?

Is libreference-ril.so from default platform package reliable and can
be used to provide phone functions in Android? Or one has to develop
vendor library?

I added GSM modem to the board via tty device but seems default RIL
library is not capable to provide phone operation. The following is
taken from radio log (by typing command: adb logcat  radio)

I/RIL     ( 1834): Opening tty device /dev/ttyS0
I/PHONE   ( 1944): Network Mode set to 0
I/PHONE   ( 1944): Cdma Subscription set to 1
D/PHONE   ( 1944): mDoesRilSendMultipleCallRing=true
D/PHONE   ( 1944): mCallRingDelay=3000
I/RILC    ( 1834): libril: new connection
I/RILC    ( 1834): RIL Daemon version: android reference-ril 1.0
I/RILJ    ( 1944): Connected to 'rild' socket
D/RILJ    ( 1944): [UNSL]< UNSOL_RESPONSE_RADIO_STATE_CHANGED
RADIO_UNAVAILABLE
W/GSM     ( 1944): Can't open /system/etc/voicemail-conf.xml
W/GSM     ( 1944): Can't open /system/etc/spn-conf.xml
D/GSM     ( 1944): [DSAC DEB] registerForPsRestrictedEnabled
D/GSM     ( 1944): [DSAC DEB] registerForPsRestrictedDisabled
D/GSM     ( 1944): [PdpConnection] DataConnection.clearSettings()
D/RILJ    ( 1944): [0000]> RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING
D/RIL     ( 1834): onRequest: <unknown request>
D/RILJ    ( 1944): [0000]< RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING
error: com.android.internal.telephony.CommandException:
RADIO_NOT_AVAILABLE
D/STK     ( 1944): StkService: StkService: is running
D/STK     ( 1944): StkService: NEW sInstance


As one can see from these two lines

D/RIL     ( 1834): onRequest: <unknown request>
D/RILJ    ( 1944): [0000]< RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING
error:

libreference-ril.so doesn't recognize
RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING message.

In the source file of the library hardware/ril/reference-ril/reference-
ril.c in the method onRequest() :

    if (sState == RADIO_STATE_UNAVAILABLE
        && request != RIL_REQUEST_GET_SIM_STATUS
    ) {
        RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
        return;
    }

Initial value of sState is RADIO_STATE_UNAVAILABLE therefore the only
message which can be processed by the RIL as the very first message
must be RIL_REQUEST_GET_SIM_STATUS.

But in fact RILJ (com.android.phone) application sends ro RILD first
message of type RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING and throws
exception getting error code RIL_E_RADIO_NOT_AVAILABLE.

Anybody knows where can be found reliable library?

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

3. wifi not remembering the access point

4. ContextWrapper.openOrCreateDatabase() throws NullPointerException

5. pop-up/toast notification without activity/context

6. how to block buttons with a layout above all?

7. Cupcake(API3) friendly way to pause/stop sound loops in SoundPool? Any ideas?