加载没有互联网的页面时出现webview错误

时间:2014-08-22 08:01:04

标签: android android-webview

我是Android新手..我编码和Android应用程序在webview中查看网页.. 如果不存在互联网,它将加载默认活动..或者它将加载网页..但是如果通过webview浏览,并且互联网进行并且重新加载工作它加载错误活动..但是在那么短的秒之间默认没有找到页面错误类似于浏览器来了一秒钟..我该怎么做..请帮帮我..我真的被困在这里......

    public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true); 
    check = new CheckInterNetConnection(getApplicationContext());
    isInternetPresent = check.isConnectingToInternet();
    setContentView(R.layout.main);

    CookieManager cookieManager = CookieManager.getInstance(); 
    cookieManager.setAcceptCookie(true); 
 mainWebView = (WebView) findViewById(R.id.mainWebView);

    WebSettings webSettings = mainWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mainWebView.setWebViewClient(new MyCustomWebViewClient());
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
   mainWebView.setWebViewClient(new WebViewClient() {
        String desc="Oops something went Wrong";
           public void onReceivedError(WebView view, int errorCode, String desc, String failingUrl) {
            // Toast.makeText(AndroidMobileAppSampleActivity.this, "Oh no! " + desc, Toast.LENGTH_SHORT).show();
            Intent i = new Intent(AndroidMobileAppSampleActivity.this, error.class);
                        startActivity(i);
                         finish(); 
           }
         });
   if(isInternetPresent)
   {

            mainWebView.loadUrl("http://www.mybuzzin.in/lite");
    }

 else{
        //no connection
          Toast toast = Toast.makeText(AndroidMobileAppSampleActivity.this, "No Internet Connection", Toast.LENGTH_LONG);
          toast.show();  
}


}   

@Override
public void onBackPressed()
{
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Exit!")
        .setMessage("Are you sure you want to Exit?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();    
            }

        })
        .setNegativeButton("No", null)
        .show();    
    }   

private class MyCustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        setProgressBarIndeterminateVisibility(true);
        return true;
    }
    @Override
    public void onPageFinished(WebView webview, String url){
        super.onPageFinished(webview, url);
        setProgressBarIndeterminateVisibility(false);
     }
}

}

2 个答案:

答案 0 :(得分:0)

您可以从资产

显示自己的错误html页面
mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mWebView.loadUrl("file:///android_asset/errorpage.html");

            }
        });

否则,您可以创建自定义Web视图并阻止加载

试试这个https://stackoverflow.com/a/6623202/3020568

答案 1 :(得分:0)

这很简单

您可以在请求网址之前检查互联网连接

 final ConnectivityManager connMgr = (ConnectivityManager) YourClass.this
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isConnected() || mobile.isConnected()) {

 webView.loadUrl(url);  

}else{
startActivity(new Intent(This.class,other.class));
}

//所需权限

将这些权限添加到清单文件

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

获取更多帮助,请在rajeshsaini890 gmail

上与我联系