Load second URL if first is not present in android webView

冷暖自知 提交于 2021-01-29 20:25:09

问题


I was making a webview application for my local network. I have two static IPs. All I want to do is to load second if 1st one is down. (Because one of them is always running).

What I'm doing is to check it in onRecievedError() function and change the URL value, then call the onCreate() again like this.

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());
}

the onCreate() method has to render the new URL but my application closes itself upon this call. Why is this so? Please help because I'm a beginner.


回答1:


use this code

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);
        }
    }


来源:https://stackoverflow.com/questions/57351203/load-second-url-if-first-is-not-present-in-android-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!