【问题标题】:how do i load another link if the first one is not available? in c#如果第一个链接不可用,我如何加载另一个链接?在c#中
【发布时间】:2018-06-17 21:52:46
【问题描述】:

如果 127.0.0.1 不可用,我想加载 www.google.com,我该怎么做?我正在使用 C#,我是编程新手,请帮忙!
这是我的代码

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Webkit;

namespace MyApp
{
  [Activity(Label = "MyApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    WebView myWebView;
    protected override void OnCreate(Bundle bundle)
    {
        SetContentView(Resource.Layout.Main);
        base.OnCreate(bundle);

        myWebView = FindViewById<WebView>(Resource.Id.webView);
        myWebView.SetWebViewClient(new WebViewClient());
        myWebView.Settings.JavaScriptEnabled = true;

       //my first link
        myWebView.LoadUrl("http://127.0.0.1");

        // my second link
        myWebView.LoadUrl("http://www.google.com");

    }
    public override void OnBackPressed()
    {
        base.OnBackPressed();
        }
    }
}

【问题讨论】:

  • 到目前为止,您为检查第一个请求的状态做了哪些尝试?

标签: c# android visual-studio


【解决方案1】:

首先你应该编写一个方法来检查你的服务器是否可用

    public static bool CheckIfServerIsAwake(string url)
    {
        var ping = new Ping();
        var reply = ping.Send(url, 60 * 1000); // 1 minute time out (in ms)
                                                        // or...
        //reply = ping.Send(new IPAddress(new byte[] { 127, 0, 0, 1 }), 3000);
        return reply.Status == IPStatus.Success;
    }

不仅仅是决策

myWebView.LoadUrl((CheckIfServerIsAwake("http://127.0.0.1") ?"http://127.0.0.1": "http://google.com"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多