我如何在 WEB VIEW 中加载失败的情况下将前一个网页更改为我的网页?

huangapple go评论71阅读模式
英文:

How can I change the previous web page to my own if it did not load in WEB VIEW?

问题

以下是您要翻译的内容:

问题在于当加载新页面时,WEBVIEW会显示先前的页面。如果页面根本没有加载或加载出现错误,则会显示“网页不可用”的消息,我不希望看到这个消息,而是想要隐藏它或替换为自己的消息。如何实现这一点?

<https://cloud.mail.ru/public/321U/4DU3gj9dy> 这是我的Android Studio项目。
存档密码:myapps123

要查找并查看错误,请重新构建项目,并按照以下步骤操作:
将应用安装到您的手机上/

  1. 启动应用程序。
  2. 在页面上打开任何链接。
  3. 关闭互联网,但不要最小化应用程序!
  4. 打开任何链接。
  5. 点击两次重新加载。
  6. 打开互联网,但不要最小化应用程序!(在您的设备上按住主页按钮)
  7. 使用防火墙阻止应用程序访问互联网。
  8. 在应用程序菜单栏中单击主页按钮。
  9. 点击屏幕。
  10. 点击重新加载,查看错误 - 我想要隐藏这个错误,或者在我的屏幕或页面上替换它。
英文:

P.S my English very bad and I have mistakes in text))) please sorry!
The problem is that when a new page is loaded, WEBVIEW shows the previous one, and if it is not loaded at all or loaded with errors, then it shows the message The web page is not available, which I would not want to see at all, but hide or replace it with my own, how is this possible to implement?
<https://cloud.mail.ru/public/321U/4DU3gj9dy> this is my Android Studio project.
archive password : myapps123

TO FIND and see the error rebuild the project and follow the steps below:
Install the app to your mobile phone/

  1. Start the application.
  2. Open any link on the page.
  3. Turn off the Internet, but do not minimize the application!
  4. Open any link.
  5. Click Reload twice
  6. Turn on the Internet, but do not minimize the application!(hold the home button in tour device 我如何在 WEB VIEW 中加载失败的情况下将前一个网页更改为我的网页?
  7. Block the application accessing to the Internet with a firewall.
  8. Click on the house button in application menu bar.
    9.click on the screen.
  9. Click Reload and see an error - I want to hide this error or replace it on my screen or page.

答案1

得分: 0

你可以在onReceivedError函数中调用loadErrorPage(view)函数。

以下代码将加载您需要显示的错误内容。在这里,我使用loadDataWithBaseURL加载HTML文件。

public void loadErrorPage(WebView webview){
    if(webview!=null){

        String htmlData = "<html><body><div align=\"center\">This is the description for the load fail : " 
                         + description + "\nThe failed url is : " + failingUrl + "\n</div></body></html>";

        webview.loadUrl("about:blank");
        webview.loadDataWithBaseURL(null,htmlData, "text/html", "UTF-8",null);
        webview.invalidate();

    }
}

您可以按以下方式编写onReceivedError:

WebView wv = (WebView) findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        // 在这里调用loadErrorPage函数
    }
});
英文:

You can call loadErrorPage(view) function in the onReceivedError function.

The following code will load the error content you need to show.Here i am load the html file with loadDataWithBaseURL.

       public void loadErrorPage(WebView webview){
            if(webview!=null){
    
                String htmlData =&quot;&lt;html&gt;&lt;body&gt;&lt;div align=\&quot;center\&quot; &gt;&quot;This is 
       the description for the load fail : &quot;+description+&quot;\nThe failed url is 
       : &quot;+failingUrl+&quot;\n&quot;&lt;/div&gt;&lt;/body&gt;&quot;;
    
                webview.loadUrl(&quot;about:blank&quot;);
                webview.loadDataWithBaseURL(null,htmlData, &quot;text/html&quot;, &quot;UTF-8&quot;,null);
                webview.invalidate();
    
            }
        }

You can write onReceivedError like that :

    WebView wv = (WebView) findViewById(R.id.webView);
     wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                su&#229;per.onReceivedError(view, errorCode, description, failingUrl);
           //call loadErrorPage function here
        }
     });

huangapple
  • 本文由 发表于 2020年10月5日 15:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64204088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定