如果第一个 URL 不存在于 android webView 中,则加载第二个 URL

Posted

技术标签:

【中文标题】如果第一个 URL 不存在于 android webView 中,则加载第二个 URL【英文标题】:Load second URL if first is not present in android webView 【发布时间】:2019-12-12 12:59:03 【问题描述】:

我正在为我的本地网络制作一个 webview 应用程序。我有两个静态 IP。我想做的就是在第一个失败的情况下加载第二个。 (因为其中一个始终在运行)。

我正在做的是在onRecievedError()函数中检查它并更改URL值,然后像这样再次调用onCreate()

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
    Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
    url1="http://192.168.43.XXX";  //here XXX is used just to hide my IP from public
    onCreate(new Bundle());

onCreate() 方法必须呈现新 URL,但我的应用程序在此调用时自行关闭。为什么会这样? 请帮忙,因为我是初学者。

【问题讨论】:

【参考方案1】:

使用此代码

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
    
        Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();

        if(failingUrl.equals(url1))
        
            //you can use load url if fail first url
            // dont't use onCreate again
            webview.loadUrl(url2);
        
    

【讨论】:

我再次调用它是因为我在其中做了很多其他的事情,比如下载和上传处理程序。

以上是关于如果第一个 URL 不存在于 android webView 中,则加载第二个 URL的主要内容,如果未能解决你的问题,请参考以下文章

如何检查图片是不是存在于url

a specific URL

如果 URL 已存在于数据库中,如何使用 PHP 检查?

如果第一个 css 课程不存在,如何申请其他 css 课程?

索引视图中完全外连接的替代方案

Android WebView err_unknown_url_scheme