故障转移时如何将Webview URL更改为另一个URL

时间:2019-05-13 16:30:21

标签: android webview android-webview

在Android Webview中,当第一个URL失败时,如何将Webview URL更改为另一个URL。

例如 我有2个网址-url1和url2

在webview的URL加载中,我放置了url1

无论如何,如果url1关闭,则我的webview网址应更改为url2

Mainacivity.java

public void WebAction(){
        myWebView = (WebView)findViewById(R.id.mywebview);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        myWebView.loadUrl("http://url1");
        swipe.setRefreshing(true);
        myWebView.setWebViewClient(new WebViewClient());
        AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this);
        appUpdateChecker.checkForUpdate(false); //mannual check false here
        myWebView.setWebViewClient(new WebViewClient(){
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                return false;
            }

            public  void  onReceivedError(WebView view, int errorCode, String description, String fallingUrl) {
                myWebView.loadUrl("file:///android_asset/error.html");
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon){
                progressBar.setVisibility(View.VISIBLE);
                setTitle("Loading.....");
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url){
                progressBar.setVisibility(View.GONE);
                setTitle(view.getTitle());
                super.onPageFinished(view, url);
                swipe.setRefreshing(false);
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

只需更改onReceviedErrorMethod

public void onReceivedError(WebView视图,int errorCode,字符串描述,字符串fallingUrl){                 myWebView.loadUrl(“输入第二个网址”);             }