protected async Task <T> GetAsync <T>(string resource, Xamarin.Essentials.NetworkAccess networkAccess, int cacheDuration = 24) { //Get Json data (from Cache or Web) var json = await GetJsonAsync(resource, networkAccess, cacheDuration); //Return the result return(JsonConvert.DeserializeObject <T>(json)); }
private async Task <string> GetJsonAsync(string resource, Xamarin.Essentials.NetworkAccess networkAccess, int cacheDuration = 24) { var cleanCacheKey = resource.CleanCacheKey(); //Try Get data from Cache var cachedData = _cacheBarrel.Get <string>(cleanCacheKey); if (cacheDuration > 0 && cachedData != null) { //If the cached data is still valid if (!_cacheBarrel.IsExpired(cleanCacheKey)) { return(cachedData); } } //Check for internet connection and return cached data if possible if (networkAccess != NetworkAccess.Internet) { if (cachedData != null) { return(cachedData); } else { throw new InternetConnectionException(); } } //No Cache Found, or Cached data was not required, or Internet connection is also available //Extract response from URI var response = await _httpClient.GetAsync(new Uri(_httpClient.BaseAddress, resource)); response.EnsureSuccessStatusCode(); //Read Response string json = await response.Content.ReadAsStringAsync(); //Save to Cache if required if (cacheDuration > 0) { try { _cacheBarrel.Add(cleanCacheKey, json, TimeSpan.FromHours(cacheDuration)); } catch { } } //Return the result return(json); }
private void Ontimedevent(object sender, ElapsedEventArgs e) { Xamarin.Essentials.NetworkAccess networkAccess = Connectivity.NetworkAccess; if (!SD.Contains(GPS.GetAddress()) && networkAccess == Xamarin.Essentials.NetworkAccess.Internet && LocationManager.IsProviderEnabled(LocationManager.GpsProvider)) { Notification.Builder notificationBuilder = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.ic_explore_black_18dp) .SetContentIntent(pendingIntent) .SetContentTitle("Explore The World") .SetContentText("You have Found Something"); var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.Notify(NOTIFICATION_ID, notificationBuilder.Build()); } }
public async void SizedButton_Clicked(object sender, EventArgs e) { //JsonValue value = JsonValue.Parse(@"{ ""name"":""Prince Charming"", ..."); //JsonObject result = value as JsonObject; Location startLocation = await Geolocation.GetLastKnownLocationAsync(); NetworkAccess current = Connectivity.NetworkAccess; if (current == NetworkAccess.Internet) { double shortestDistance = 0; Models.Directions directions = new Models.Directions(); Location neareatPark = new Location(); foreach (Pin pin in MMap.Pins) { Location endLocation = new Location(pin.Position.Latitude, pin.Position.Longitude); Models.Directions tempDirections = await DirectionsMethods.GetDirectionsInfo(startLocation.Latitude, endLocation.Latitude, startLocation.Longitude, endLocation.Longitude); if (tempDirections != null) { double tempDistance = DirectionsMethods.GetDistance(tempDirections); if (shortestDistance == 0 || shortestDistance > tempDistance) { neareatPark = endLocation; shortestDistance = tempDistance; directions = tempDirections; } } } //map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(neareatPark.Latitude, neareatPark.Longitude), // Distance.FromKilometers(.1))); //foreach (Models.Route route in directions.Routes) //{ // foreach (Models.Leg leg in route.legs) // { // map.LoadRoutes(leg.steps); // } //} foreach (Models.Route route in directions.Routes) { MMap.LoadRoutes(route.overview_polyline); } } }
private NetworkStatus GetNetworkStatus(Xamarin.Essentials.NetworkAccess status, IEnumerable <Xamarin.Essentials.ConnectionProfile> profiles) { NetworkStatus networkInfo = 0; switch (status) { case Xamarin.Essentials.NetworkAccess.None: return(NetworkStatus.None); case Xamarin.Essentials.NetworkAccess.Internet: networkInfo = NetworkStatus.Internet; break; case Xamarin.Essentials.NetworkAccess.ConstrainedInternet: case Xamarin.Essentials.NetworkAccess.Local: networkInfo = NetworkStatus.Local; break; default: return(NetworkStatus.None); } // Add more info in case we have a connection. if (profiles.Any(p => p == Xamarin.Essentials.ConnectionProfile.WiFi)) { networkInfo |= NetworkStatus.Wifi; } if (profiles.Any(p => p == Xamarin.Essentials.ConnectionProfile.Ethernet)) { networkInfo |= NetworkStatus.Ethernet; } if (profiles.Any(p => p == Xamarin.Essentials.ConnectionProfile.Bluetooth)) { networkInfo |= NetworkStatus.Bluetooth; } if (profiles.Any(p => p == Xamarin.Essentials.ConnectionProfile.Cellular)) { networkInfo |= NetworkStatus.Cellular; } return(networkInfo); }
public string Getinfo(string address, string country) { Xamarin.Essentials.NetworkAccess networkAccess = Connectivity.NetworkAccess; if (networkAccess != Xamarin.Essentials.NetworkAccess.Internet) { return("No Internet Connection can be made"); } if (address == "Can't Find location") { return("No GPS availible"); } string[] importtext; string URL = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=" + address; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); string importstring = reader.ReadLine(); JSONObject WIKI = new JSONObject(importstring); JSONObject query = WIKI.GetJSONObject("query"); JSONObject pages = query.GetJSONObject("pages"); string[] Psplit = pages.ToString().Split(':'); string trimP = Psplit[0]; trimP = trimP.Trim('"', '{', '\\'); if (pages.ToString().Contains("extract") == true && !importstring.Contains("may mean") && !importstring.Contains("may refer")) { JSONObject ID = pages.GetJSONObject(trimP); return(ID.GetString("extract")); } importtext = importstring.Split(':'); importstring = ""; importtext[0] = ""; importtext[1] = ""; importtext[2] = ""; importtext[3] = ""; importtext[4] = ""; importtext[5] = ""; importtext[6] = ""; importtext[7] = ""; foreach (string str in importtext) { if (str.Contains("may mean") || str.Contains("may refer")) { WikipediaAPI wapi = new WikipediaAPI(); //string[] split = importtext[9].Split(','); //string teststring = importtext[9]; return(wapi.Getinfo(address + ",_" + country, country)); } } foreach (string str in importtext) { importstring = importstring + str; } string finalstring = ""; char oldchar = ' '; foreach (char cr in importstring) { if (cr != '{' && cr != '}' && cr != '[' && cr != ']' && cr != '\\' && cr != ';') { if (oldchar != '\\') { finalstring = finalstring + cr; } else { finalstring = finalstring + ' '; } } oldchar = cr; } return(finalstring); } }