如果Android中没有互联网连接,如何显示错误页面

时间:2015-12-04 17:23:52

标签: java android webview

我想从我创建的资产文件夹中加载错误页面。一切都运作顺利,但错误页面,即没有互联网的情况下加载404.html。我在MainActivity.java文件中使用以下Snippet。

public class MainActivity extends ActionBarActivity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_asset/www/index.html");
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebViewClient(new MyAppWebViewClient());

        mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mWebView.loadUrl("file:///android_asset/www/404.html");

            }
        });

2 个答案:

答案 0 :(得分:2)

使用此方法检查互联网连接:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

如果它返回false,则显示您的错误页面

你需要

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

清单中的ACCESS_NETWORK_STATE权限

编辑这样做:

public class MainActivity extends ActionBarActivity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        if(isNetworkAvailable())
            mWebView.loadUrl("file:///android_asset/www/index.html");
        else
            mWebView.loadUrl("file:///android_asset/www/404.html");
    }

    private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

答案 1 :(得分:0)

您可以使用以下编码检查互联网连接。

WebSettings wbset=wbb.getSettings(); wbset.setJavaScriptEnabled(true); wbb.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) { view.loadUrl("about:blank"); Toast.makeText(MainActivity.this, "Error occurred, please check network connectivity", Toast.LENGTH_SHORT).show(); super.onReceivedError(view, errorCode, description, failingUrl); }