【问题标题】:Load second URL if first is not present in android webView如果第一个 URL 不存在于 android webView 中,则加载第二个 URL
【发布时间】: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,但我的应用程序在此调用时自行关闭。为什么会这样? 请帮忙,因为我是初学者。

【问题讨论】:

    标签: android android-studio webview


    【解决方案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);
            }
        }
    

    【讨论】:

    • 我再次调用它是因为我在其中做了很多其他的事情,比如下载和上传处理程序。
    猜你喜欢
    • 2021-10-12
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多