1

It is unclear how to implement navigation/internet connection error.

I have tried code from here Android WebView onReceivedError()

but it does not work.

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private WebView view;
    private ImageView splashScreen;
    private ImageView logo;

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        splashScreen =   (ImageView) this.findViewById(R.id.spscreen);

        logo =  (ImageView) this.findViewById(R.id.logo);

        String url = "some URL";
        view = (WebView)this.findViewById(R.id.webView1);

        view.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                logo.setVisibility(View.INVISIBLE);
                splashScreen.setVisibility(View.INVISIBLE);
                view.setVisibility(View.VISIBLE);
            }
        });


        WebSettings s = view.getSettings();
        s.setJavaScriptEnabled(true);

        s.setCacheMode(WebSettings.LOAD_DEFAULT);
        s.setDomStorageEnabled(true);


        view.loadUrl(url);
    }
}
Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • i don't see an override of `onReceivedError()` in your code, also note, the method in the answer above was deprecated, there is another flavor. https://developer.android.com/reference/android/webkit/WebViewClient.html and there is `onReceivedHttpError()` too, could be what you need – Yazan Mar 16 '17 at 13:33
  • @Yazan Would you mind to suggest some solution please? – NoWar Mar 16 '17 at 14:15
  • 1
    yes, i already suggested using the non-depricated flavor of `onReceivedError()` as it's not clear from your code if u used it or used the deprecated one. also there is another method `onReceivedHttpError()` try to orverride it. -- that's all i can help with – Yazan Mar 16 '17 at 14:22
  • @Yazan Well...I just need to show message if there is no Internet and thats it. Would you suggest some code to do it, pls? – NoWar Mar 16 '17 at 14:37
  • ahh, sure i got you know, you simply need to implement this method http://stackoverflow.com/a/4009133/3604083 and use it at `onCreate()` or before calling `view.loadUrl(url);` ex, `if(!isOnline()){toast(not connected)...}else{view.loadUrl(url);}` – Yazan Mar 16 '17 at 14:41
  • now i got what you mean, i have posted an answer, hope it helps you – Yazan Mar 16 '17 at 14:50

1 Answers1

2

Well, there are 2 options for you on this

either let the loadUrl() start and handle the error later using onReceivedError()

view.setWebViewClient(new WebViewClient() {
    @override
    public void onPageFinished(WebView view, String url) {
        // do your stuff here
        logo.setVisibility(View.INVISIBLE);
        splashScreen.setVisibility(View.INVISIBLE);
        view.setVisibility(View.VISIBLE);
    }

    @override
    void onReceivedError (WebView view, 
        WebResourceRequest request, 
        WebResourceError error){
            if(error.getErrorCode () == WebViewClient.ERROR_CONNECT || error.getErrorCode () == WebViewClient.ERROR_HOST_LOOKUP || WebViewClient.ERROR_TIMEOUT){
                Toast.makeText(context, "you are not connected!", Toast.LENGTH_LONG);
                //do other stuff, hide views ...
            }
        }
});

tried to handle all connectivity errors, full list of error codes can be found here

Or use the isOnline() method from this answer to wrap loadUrl() call requires permission android.permission.ACCESS_NETWORK_STATE

if(!isOnline()){
    Toast.makeText(context, "you are not connected!", Toast.LENGTH_LONG);
}else{
    view.loadUrl(url);
}

if first option is what you need, it could be better, because being connected to Wifi without actual internet connection will give you false result.

Community
  • 1
  • 1
Yazan
  • 6,074
  • 1
  • 19
  • 33