Tag : xamarin-forms
Tag : xamarin-forms
Map is an external component which will be obtained from the Nuget Component store.
Integration of Maps requires some config changes from platform to platforms. I have targeted the WinPhone project configuration here. In the next blog, will point to other platforms.
Simple Steps
In the PCL/Shared project, Let’s add a new Content page and name it as MapPage.cs. Now time to add the below code to it.
Categories: Cross Platform Mobile App, CSharp, Visual Studio, Windows Phone, Xamarin
Web service is an important part of the mobile app. I am trying to consume a RESTFul web service in Xamarin using HttpClient.
Prerequisites :
– Microsoft.Net.Http
– Newtonsoft.Json
In this example, let’s display the name of the places for a given postal code. We will use the http://api.geonames.com webservice to get the places.
Call using GET method
Now let’s create the UI having a Button and a Listview in it. Button will contain the code to fetch the web service on click event and Listview will display the result.
We are with the UI, which is equipped with the Listview and the Button. The newButn_Clicked() method is associated with the Button click event to get the data from the Web service. Now, let’s define the code to fetch data from the WebService. Since webservice will return JSON we will decode the JSON using the Newtonsoft.Json.
As mentioned above, webservice will return the JSON and we have used the DeserializeObject<>() method to deserialize the JSON to a model object. Model class would look like as:
The PlaceObject class will get the array of Place class after the JSON deserization process. So we have marked the places attribute as the array name returned by the web service i.e. “postalcodes”.
We can now run the code to see the data fetched from the webservice in separate platforms.
Collectively the code looks like :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; namespace FeedShowingApp { public class App { static ListView lstPlaces = new ListView(); public static Page GetMainPage() { Button newButn = new Button() { Text = "Connect to Service", }; newButn.Clicked += newButn_Clicked; lstPlaces.ItemTemplate = new DataTemplate(typeof(TextCell)); lstPlaces.ItemTemplate.SetBinding(TextCell.TextProperty, "placeName"); return new ContentPage { Content = new StackLayout() { Children = { newButn, lstPlaces } } }; } static async void newButn_Clicked(object sender, EventArgs e) { GeoNamesWebService geoService = new GeoNamesWebService(); Place[] places= await geoService.GetPlacesAsync(); lstPlaces.ItemsSource = places; } } public class GeoNamesWebService { public GeoNamesWebService() { } public async Task<Place[]> GetPlacesAsync() { var client = new System.Net.Http.HttpClient(); client.BaseAddress = new Uri("http://api.geonames.org/"); var response = await client.GetAsync("postalCodeLookupJSON?postalcode=751010&country=IN&username=nirmalh"); var placesJson = response.Content.ReadAsStringAsync().Result; Placeobject placeobject = new Placeobject(); if(placesJson!="") { placeobject = JsonConvert.DeserializeObject<Placeobject>(placesJson); } return placeobject.places; } } public class Placeobject { [JsonProperty("postalcodes")] public Place[] places { get; set; } } public class Place { public string placeName { get; set; } } } |
Call using POST method
To call the web service in POST method, we will replace the GetAsync() method with PostAsync() method as follows
1 2 3 4 |
StringContent str = new StringContent("postalcode=751010&country=IN&username=nirmalh", Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await client.PostAsync(new Uri("http://api.geonames.org/postalCodeLookupJSON"), str); |
Code for the POST method will look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; namespace FeedShowingApp { public class App { static ListView lstPlaces = new ListView(); public static Page GetMainPage() { Button newButn = new Button() { Text = "Connect to Service", }; newButn.Clicked += newButn_Clicked; lstPlaces.ItemTemplate = new DataTemplate(typeof(TextCell)); lstPlaces.ItemTemplate.SetBinding(TextCell.TextProperty, "placeName"); return new ContentPage { Content = new StackLayout() { Children = { newButn, lstPlaces } } }; } static async void newButn_Clicked(object sender, EventArgs e) { GeoNamesWebService geoService = new GeoNamesWebService(); Place[] places= await geoService.GetPlacesAsync(); lstPlaces.ItemsSource = places; } } public class GeoNamesWebService { public GeoNamesWebService() { } public async Task<Place[]> GetPlacesAsync() { var client = new System.Net.Http.HttpClient(); client.BaseAddress = new Uri("http://api.geonames.org/"); StringContent str = new StringContent("postalcode=752020&country=IN&username=nirmalh", Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await client.PostAsync(new Uri("http://api.geonames.org/postalCodeLookupJSON"), str); var placesJson = response.Content.ReadAsStringAsync().Result; Placeobject placeobject = new Placeobject(); if(placesJson!="") { placeobject = JsonConvert.DeserializeObject<Placeobject>(placesJson); } return placeobject.places; } } public class Placeobject { [JsonProperty("postalcodes")] public Place[] places { get; set; } } public class Place { public string placeName { get; set; } } } |
Happy coding 🙂
Categories: Cross Platform Mobile App, iOS, Visual Studio, Windows Phone, Xamarin
What is DependencyService in Xamarin ?
DependencyService is a static class under Xamarin.Forms namespace. Shared or Portable projects can access the platform based tweaks that are done in any specific platform projects by using DependencyService.
How it works?
The key point is by defining an Interface.
Categories: Android, Cross Platform Mobile App, iOS, Windows Phone, Xamarin