如果第一个URL不存在于android webView中,请加载第二个URL [英] Load second URL if first is not present in android webView

查看:54
本文介绍了如果第一个URL不存在于android webView中,请加载第二个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的本地网络制作一个webview应用程序.我有两个静态IP.我要做的就是在第一个关闭的情况下加载第二个.(因为其中之一一直在运行).

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).

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

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

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

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.

推荐答案

使用此代码

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆