IsHostReachable() public static method

public static IsHostReachable ( string host ) : bool
host string
return bool
Exemplo n.º 1
0
        private static string ReverseGeocode(double lat, double lon)
        {
            string res = null;

            try {
                if (Reachability.IsHostReachable("www.google.com"))
                {
                    WebClient client = new WebClient();
                    string    output = client.DownloadString(string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=true", lat, lon));

                    XElement root = XElement.Parse(output);

                    var items = from item in root.Descendants("result")
                                select new
                    {
                        AddressType      = item.Element("type").Value,
                        FormattedAddress = item.Element("formatted_address").Value
                    };

                    foreach (var item in items)
                    {
                        string[] parts = item.FormattedAddress.Split(',');

                        if (parts.Length >= 2)
                        {
                            res = string.Format("{0}, {1}", parts [0].Trim(), parts [1].Trim());
                            return(res);
                        }
                        else
                        {
                            res = item.FormattedAddress;
                            return(res);
                        }
                    }
                }

                return("Unknown");
            } catch (Exception ex) {
                Util.Log("Exception in reverse geocode: {0}", ex.Message);
                return("Unknown");
            }

            return("Unknown");
        }
Exemplo n.º 2
0
        public void LoadFromWeb()
        {
            ThreadPool.QueueUserWorkItem(delegate {
                using (NSAutoreleasePool pool = new NSAutoreleasePool())
                {
                    try {
                        Util.TurnOnNetworkActivity();

                        if (Reachability.IsHostReachable("app.londonbikeapp.com"))
                        {
                            Util.Log("Downloading info");
                            WebClient client  = new WebClient();
                            string infopage   = client.DownloadString("http://www.fastchicken.co.nz/lba/infopage.xml");
                            string updateinfo = client.DownloadString("http://www.fastchicken.co.nz/lba/update_times.txt");

                            string minTime    = "";
                            string maxTime    = "";
                            string lastUpdate = "";
                            XElement root     = null;
                            try
                            {
                                root    = XElement.Parse(updateinfo);
                                minTime = root.Element("min_update_time").Value;
                                maxTime = root.Element("max_update_time").Value;


                                Util.Log("min: {0} max: {1}", minTime, maxTime);
                            } catch
                            {
                                minTime = "";
                                maxTime = "";
                            }

                            Util.Log("parsing");
                            root = XElement.Parse(infopage);

                            RootElement rootElement = new RootElement("Information");

                            bool isFirst = true;

                            foreach (var xmlSection in root.Descendants("section"))
                            {
                                Section section = new Section();

                                try
                                {
                                    maxTime = maxTime.Substring(0, maxTime.LastIndexOf(".")) + " +00:00";
                                    DateTime maxDatetime = DateTime.ParseExact(maxTime, "yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture);

                                    string footer = string.Format("Realtime data updated  @ {0}", maxDatetime.ToString("HH:mm"));

                                    if (isFirst)
                                    {
                                        section = new Section("", footer);
                                    }
                                } catch (Exception ex)
                                {
                                }

                                var items = from item in xmlSection.Elements("item")
                                            select new {
                                    Title    = item.Attribute("title") == null ? "" : item.Attribute("title").Value,
                                    Subtitle = item.Attribute("subtitle") == null ? "" : item.Attribute("subtitle").Value,
                                    Url      = item.Attribute("url") == null ? "" : item.Attribute("url").Value,
                                    LongText = item.Attribute("longtext") == null ? "" : item.Attribute("longtext").Value
                                };

                                isFirst = false;

                                foreach (var item in items)
                                {
                                    var localItem = item;

                                    //Util.Log("found item: {0}, {1}", localItem.Title, localItem.Subtitle);

                                    if (String.IsNullOrEmpty(localItem.LongText))
                                    {
                                        section.Add(new DetailStringElement(localItem.Title, localItem.Subtitle, delegate {
                                            Util.LoadUrl(localItem.Url);
                                        }));
                                    }
                                    else
                                    {
                                        section.Add(new StyledMultilineElement(localItem.LongText)
                                        {
                                            Font = Resources.DetailFont                                             //, TextColor = Resources.DetailColor
                                        });
                                    }
                                }



                                rootElement.Add(section);
                            }

                            InvokeOnMainThread(delegate { Root = rootElement; });
                        }
                    } finally {
                        Util.TurnOffNetworkActivity();
                    }
                }
            });
        }
Exemplo n.º 3
0
 public static bool IsReachable(string host)
 {
     return(Reachability.InternetConnectionStatus() != NetworkStatus.NotReachable &&
            Reachability.IsHostReachable(host));
 }