如何修复“ Adroid Webview频繁加载错误页面”?

时间:2019-08-16 15:58:40

标签: android webview android-webview

我的Webview应用一旦检测到错误(无Internet连接),便会加载错误页面(.html)。如果没有连接或网络较弱,则会立即发生。

我的代码如下

MainActivity.xml

package *.*.*;

import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import *.*.*.R;

public class MainActivity extends AppCompatActivity {
    private WebView MyWebView;
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    SwipeRefreshLayout swipe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigationView = (NavigationView) findViewById(R.id.navigation_menu);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.nav_home:
                        MyWebView.loadUrl("http://***.com");
                        break;
                    case R.id.nav_allproducts:
                        MyWebView.loadUrl("http://***.com/index.php?route=product/allproduct&limit=50");
                        break;
                    case R.id.nav_newarrivals:
                        MyWebView.loadUrl("http://***.com/index.php?route=product/latest");
                        break;
                    case R.id.nav_login:
                        MyWebView.loadUrl("http://***.com/index.php?route=account/login");
                        break;
                    case R.id.nav_myaccount:
                        MyWebView.loadUrl("http://***.com/index.php?route=account/account");
                        break;
                    case R.id.nav_mywishlist:
                        MyWebView.loadUrl("http://***.com/index.php?route=account/wishlist");
                        break;
                }
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel mChannel =
                    new NotificationChannel(Constants.CHANNEL_ID, Constants.CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

            mChannel.setDescription(Constants.CHANNEL_DESCRIPTION);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);

            mChannel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400});

            mNotificationManager.createNotificationChannel(mChannel);
        }

        MyWebView = (WebView)findViewById(R.id.webView);
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                    LoadWeb(MyWebView.getUrl());
            }
        });

        LoadWeb("http://***.com");
    }

    public void LoadWeb (String url){

        WebSettings webSettings = MyWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        MyWebView.loadUrl(url);
        swipe.setRefreshing(true);
        MyWebView.setWebViewClient(new WebViewClient() {


            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                MyWebView.loadUrl("file:///android_asset/error.html");
            }


            //For versions < Android M (API 23).
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                MyWebView.loadUrl("file:///android_asset/error.html");
            }


            public void onPageFinished(WebView view, String url) {
                //Hide the swipeRefreshLayout
                swipe.setRefreshing(false);

            }
        });
    }

    @Override
    public void onBackPressed() {
        if(MyWebView.canGoBack()) {
            MyWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

我希望它在检测到错误时保留在现有页面上,并在只有用户单击Webview中的任何链接时才显示错误页面。

1 个答案:

答案 0 :(得分:0)

在收到错误时使用它显示空白屏幕

MyWebView.loadUrl(about:blank);

onReceivedError中。