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. Quality & Affordable Jewelry, Cosmetics, Skin Care, Clothing

Online Shopping!
Jewelry, Cosmetics, Skin Care Products, Hair Care Products, Health &
Wellness Products, Clothing, Shoes, Housewares, and Childrens Products
Available.

we take PAYPAL as the method of payment!
please kindly visite our website: http://www.cnnshoe.com
msn: cnnshoe2...@hotmail.com
email: cnns...@gmail.com

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

2. How can i import com.android.internal.phone?

Hi there,
I'm trying to invoke the functions belong to class phone and
PhoneFactory. But when I'm trying to import these packages, it reminds
errors:

code:
import com.android.internal.telephony.Phone
import com.android.internal.telephony.PhoneFactory
...
PhoneFactory.makeDefaultPhones(this)
Phone phone = PhoneFactory.getDefaultPhone()

error:
The com.android.internal.telephony.Phone can not be resolved.
The com.android.internal.telephony.PhoneFactory can not be resolved.

But some other packages can be imported. I don't know why. I think all
this packages should be included in the SDK.
Can any one tell me where I am getting wrong?
Thanks.
Nio
--~--~---------~--~----~------------~-------~--~----~

3. usb connection detection

4. 1.5 SDK: "android list target" failed

5. How can i import com.android.internal.phone?

6. "send" intent only when activity is on top running

7. Accessing native service from an application